Share on Google+Share on Google+

lavanya satyavarapu
this is my javascript code and i am not understanding the mistake in this,please help me?
3 Answer(s)      4 years and 5 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>

Ads

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
Tutorials   
Java Spring Hibernate Struts Training code for programming How can I dynamically load user-entered data as content of jquery dialog? java webcam integration question java coding Fix Scrollbar position in a checkboxList after postback Information Save Alert Country wise redirection to files Interface in javascript connect database with javascript Zip Code ZipCode how to validate the email login How to play FLV video files on Ipad using HTML code or javascript code? Nitobi Grid is not working in IE10/IE11 jquery issue plese help RegEx Tree structure from json whose parents can be dragged and dropped from child newyorklife insurance getElementById not working javascript code for search in html file Implement The Skeleton Code Implement This Skeleton Code Mobile Automation using Appium Computer forensic application Java: Searching a string from doc files java linked list urgent !!! java linked list urgent!!! library management library management library management library management question Html form using JavaScript to display the table content java how to prevent no from unroundin off on clicking text box again in JSP and Jquery graph generation from xml design file display data from database in textbox when id entered in textbox Javascript Code for all fields how to get HTTP GET request value using javascript array Javascript Querry autocomplete(): Spring mvc with jquery: I am not getting correct value in the text filed. Please help me Create SpreadSheet same as excel using java applet Create SpreadSheet same as excel using java applet How to change focus among multiple select boxes? Write JQUERY Code Write JQUERY Code Converting jsp variable to java variable java

Ads

 
Advertisement null

Ads