Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa JavaScriptQuestions this is my javascript code and i am not understanding the mistake in this,please help me?

Have Programming Question? Ask it here!
 
 
 


lavanya satyavarapu
this is my javascript code and i am not understanding the mistake in this,please help me?
3 Answer(s)      a year and 9 months ago
Posted in : JavaScript Questions


<html>
    <h2>Form Validation</h2>
        <script language = "Javascript">
          function checkEmail() {
            if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(form.email.value)){
            return true;
            }
            return false; 
          }
          function checkName(str){
              var re = /[^a-zA-Z]/g
              if (re.test(str)) return false;
              return true;
            }
            function checkAddress(str){
              var re = /[^[a-z][A-Z][0-9]]/g
              if(re.test(str)) return false;
              return true;
            }
            function checkMobile(str){
                var re=/^([7-9]{1})([0-9]{9})$/g
        if(re.test(str)) return false;
        return true;
        }
          function validate(){
            var emailID=document.form.email;
            var nn1=document.form.fname;
            var nn2=document.form.lname;
            var addr1=document.form.add1;
            var addr2=document.form.add2;
            var addr3=document.form.add3;
            var gen=document.form.gender;
            var mobile=document.form.mobilenumber;
            var city=document.form.cityname;
            var state=document.form.statename;
            var zip=document.form.zip1;
            if((nn1.value==null)||(nn1.value=="")){
            alert("Please Enter your First Name!")
            nn1.focus();
            return false
            }
            if((nn2.value==null)||(nn2.value=="")){
            alert("Please Enter your Last Name!")
            nn2.focus();
            return false
            }
            if(checkName(nn1.value)==false){
            nn1.value=""
            alert("Invalid Name!");
            nn1.focus()
            return false
            }
            if(checkName(nn2.value)==false){
            nn2.value=""
            alert("Invalid Name!");
            nn2.focus()
            return false
            }
            if ( ( gen[0].checked == false ) && ( gen[1].checked == false ) ){
                alert("please check your gender");
                gen.focus();
                return false;
            }
            if((emailID.value==null)||(emailID.value=="")){
            alert("Please Enter your Email ID!")
            emailID.focus();
            return false;
            }
            if (checkEmail(emailID.value)==false){
            emailID.value=""
            alert("Invalid Email Adderess!");
            emailID.focus()
            return false
            }
            if(checkMobile(mobile.value==null)||mobile.value==""){
                alert("please Enter Valid Mobile number")
                mobile.focus();
                return false; 
           }
            if ((addr1.value==null)||(addr1.value=="")){
            alert("Please Enter your Address!")
            addr1.focus()
            return false
            }
            if (checkAddress(addr1.value)==false){
            addr1.value=""
            alert("Invalid Adderess!");
            addr1.focus()
            return false
            }
            if ((add2.value==null)||(add2.value=="")){
            alert("Please Enter your Address!")
            add2.focus()
            return false
            }
            if (checkAddress(addr2.value)==false){
            addr2.value=""
            alert("Invalid Adderess!");
            addr2.focus()
            return false
            }
            if ((addr3.value==null)||(addr3.value=="")){
            alert("Please Enter your Address!")
            addr3.focus()
            return false
            }
            if (checkAddress(addr3.value)==false){
            addr3.value=""
            alert("Invalid Adderess!");
            addr3.focus()
            return false
            }
            if((city.value==null)||(nn2.value=="")){
            alert("Please Enter your city!")
            city.focus();
            return false
            }
            if(checkName(city.value)==false){
            city.value=""
            alert("Invalid Name!");
            city.focus()
            return false
            }
            if((state.value==null)||(state.value=="")){
            alert("Please Enter your city Name!")
            state.focus();
            return false
            }
            if(checkName(state.value)==false){
            state.value=""
            alert("Invalid Name!");
            state.focus()
            return false
            }
             if( zip.value == "" || isNaN(zip.value ) ||zip.value.length != 6 ){
             alert( "Please provide a zip in the format ######." );
             zip.focus() ;
             return false;
             }
            {
            return true
          }
        </script>
        <form name="form" method="post" onSubmit="return validate()">
        <pre>
        First Name:              <input type="text" name="fname" size="30"><br>
        Last Name:               <input type="text" name="lname" size="30"><br>
        Gender: <input type="radio" name="gender" value="Male"> Male
                <input type="radio" name="gender" value="Female"> Female
                <br>
        Enter an Email Address : <input type="text" name="email" size="30"><br>
        Mobile Number:<input type="text" name="mobilenumber" size="10"><br>
        Address1:<input type="text" name="add1"><br>
        Address2:<input type="text" name="add2"><br>
        Address3:<input type="text" name="add3"><br>
        City  :<input type="text" name="cityname"><br>
        State:<input type="text" name="statename"><br>
        ZIP:<input type="text" name="zip1"><br>
        <input type="submit" name="Submit" value="Submit">
        </pre>
        </form>
</html>

Advertisement
View Answers

October 31, 2012 at 3:53 PM


Here is your required code, we have modified it.

<html>
    <h2>Form Validation</h2>
        <script language = "Javascript">
          function checkEmail() {
            if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(form.email.value)){
            return true;
            }
            return false; 
          }
          function checkName(str){
              var re = /[^a-zA-Z]/g
              if (re.test(str)) return false;
              return true;
            }
            function checkAddress(str){
              var re = /[^[a-z][A-Z][0-9]]/g
              if(re.test(str)) return false;
              return true;
            }
            function checkMobile(str){
                var re=/^([7-9]{1})([0-9]{9})$/g
        if(re.test(str)) return false;
        return true;
        }
          function validate(){
            var emailID=document.form.email;
            var nn1=document.form.fname;
            var nn2=document.form.lname;
            var addr1=document.form.add1;
            var addr2=document.form.add2;
            var addr3=document.form.add3;
            //var gen=document.form.gender;
            var mobile=document.form.mobilenumber;
            var city=document.form.cityname;
            var state=document.form.statename;
            var zip=document.form.zip1;
            if((nn1.value==null)||(nn1.value=="")){
            alert("Please Enter your First Name!")
            nn1.focus();
            return false
            }
            if(checkName(nn1.value)==false){
            nn1.value=""
            alert("Invalid Name!");
            nn1.focus()
            return false
            }
            if((nn2.value==null)||(nn2.value=="")){
            alert("Please Enter your Last Name!")
            nn2.focus();
            return false
            }
            if(checkName(nn2.value)==false){
            nn2.value=""
            alert("Invalid Name!");
            nn2.focus()
            return false
            }
            if((document.form.gender[0].checked==false )&&(document.form.gender[1].checked==false )){
                alert("please check your gender");
                document.form.gender[0].focus();
                return false;
            }
            if((emailID.value==null)||(emailID.value=="")){
            alert("Please Enter your Email ID!")
            emailID.focus();
            return false;
            }
            if(checkEmail(emailID.value)==false){
            emailID.value=""
            alert("Invalid Email Address!");
            emailID.focus()
            return false
            }


October 31, 2012 at 3:53 PM


Here is your required code, we have modified it.

<html>
    <h2>Form Validation</h2>
        <script language = "Javascript">
          function checkEmail() {
            if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(form.email.value)){
            return true;
            }
            return false; 
          }
          function checkName(str){
              var re = /[^a-zA-Z]/g
              if (re.test(str)) return false;
              return true;
            }
            function checkAddress(str){
              var re = /[^[a-z][A-Z][0-9]]/g
              if(re.test(str)) return false;
              return true;
            }
            function checkMobile(str){
                var re=/^([7-9]{1})([0-9]{9})$/g
        if(re.test(str)) return false;
        return true;
        }
          function validate(){
            var emailID=document.form.email;
            var nn1=document.form.fname;
            var nn2=document.form.lname;
            var addr1=document.form.add1;
            var addr2=document.form.add2;
            var addr3=document.form.add3;
            //var gen=document.form.gender;
            var mobile=document.form.mobilenumber;
            var city=document.form.cityname;
            var state=document.form.statename;
            var zip=document.form.zip1;
            if((nn1.value==null)||(nn1.value=="")){
            alert("Please Enter your First Name!")
            nn1.focus();
            return false
            }
            if(checkName(nn1.value)==false){
            nn1.value=""
            alert("Invalid Name!");
            nn1.focus()
            return false
            }
            if((nn2.value==null)||(nn2.value=="")){
            alert("Please Enter your Last Name!")
            nn2.focus();
            return false
            }
            if(checkName(nn2.value)==false){
            nn2.value=""
            alert("Invalid Name!");
            nn2.focus()
            return false
            }
            if((document.form.gender[0].checked==false )&&(document.form.gender[1].checked==false )){
                alert("please check your gender");
                document.form.gender[0].focus();
                return false;
            }
            if((emailID.value==null)||(emailID.value=="")){
            alert("Please Enter your Email ID!")
            emailID.focus();
            return false;
            }
            if(checkEmail(emailID.value)==false){
            emailID.value=""
            alert("Invalid Email Address!");
            emailID.focus()
            return false
            }


October 31, 2012 at 3:54 PM


continue..

if(checkMobile(mobile.value==null)||mobile.value==""){
                    alert("please Enter Valid Mobile number")
                    mobile.focus();
                    return false; 
                }
                if ((addr1.value==null)||(addr1.value=="")){
                alert("Please Enter your Address!")
                addr1.focus()
                return false
                }
                if (checkAddress(addr1.value)==false){
                addr1.value=""
                alert("Invalid Adderess!");
                addr1.focus()
                return false
                }
                if ((add2.value==null)||(add2.value=="")){
                alert("Please Enter your Address!")
                add2.focus()
                return false
                }
                if (checkAddress(addr2.value)==false){
                addr2.value=""
                alert("Invalid Adderess!");
                addr2.focus()
                return false
                }
                if ((addr3.value==null)||(addr3.value=="")){
                alert("Please Enter your Address!")
                addr3.focus()
                return false
                }
                if (checkAddress(addr3.value)==false){
                addr3.value=""
                alert("Invalid Adderess!");
                addr3.focus()
                return false
                }
                if((city.value==null)||(nn2.value=="")){
                alert("Please Enter your city!")
                city.focus();
                return false
                }
                if(checkName(city.value)==false){
                city.value=""
                alert("Invalid Name!");
                city.focus()
                return false
                }
                if((state.value==null)||(state.value=="")){
                alert("Please Enter your city Name!")
                state.focus();
                return false
                }
                if(checkName(state.value)==false){
                state.value=""
                alert("Invalid Name!");
                state.focus()
                return false
                }
                 if( zip.value == "" || isNaN(zip.value ) ||zip.value.length != 6 ){
                 alert( "Please provide a zip in the format ######." );
                 zip.focus() ;
                 return false;
                 }

                return true
              }
            </script>
            <form name="form" method="post" onSubmit="return validate();">
            <pre>
            First Name:              <input type="text" name="fname" size="30"><br>
            Last Name:               <input type="text" name="lname" size="30"><br>
            Gender: <input type="radio" name="gender" value="Male"> Male
                    <input type="radio" name="gender" value="Female"> Female
                    <br>
            Enter an Email Address : <input type="text" name="email" size="30"><br>
            Mobile Number:<input type="text" name="mobilenumber" size="10"><br>
            Address1:<input type="text" name="add1"><br>
            Address2:<input type="text" name="add2"><br>
            Address3:<input type="text" name="add3"><br>
            City  :<input type="text" name="cityname"><br>
            State:<input type="text" name="statename"><br>
            ZIP:<input type="text" name="zip1"><br>
            <input type="submit" name="Submit" value="Submit">
            </pre>
            </form>
    </html>


Related Tutorials/Questions & Answers:
Advertisements
 

 

 

DMCA.com