Pls help me out !!! :nonod: I have two text fields in a form that i need to validate to be non-empty and numeric(0-9) only.I have done the non-empty validation, but not able to do the numeric one. Here is my code- Code: <script language="JavaScript"> function onlyNumbers() { valid=true; if (document.form1.consumerno.value == "") { alert ( "Please fill in your consumer no.." ); valid = false; } if (document.form1.mobileno.value == "") { alert ( "Please fill in your mobile no.." ); valid = false; } if (document.form1.consumerno.value == "ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz") { alert ( "Numbers only" ); valid = false; } if (document.form1.mobileno.value == "ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz") { alert ( "Numbers only" ); valid = false; } return valid; } </script> And the form part as- Code: <form name="form1" action="success.html" method="post" onsubmit="return onlyNumbers();"> <br><br> <center><b>Consumer Number: <input type="text" name="consumerno"> </b></center> <br><br> <center><b>Mobile Number: <input type="text" name="mobileno" > </b></center> <br><br><br> <center> <input type="submit" name="Submit" value="Submit"> <input type="reset" name="Reset" value="Reset"></center> Can anybody help me out plssssssssss....!!! :disappoin
Change the script part to this : HTML: <script language="JavaScript"> function onlyNumbers() { valid=true; var Numbers = '0123456789'; var Field1 = document.form1.consumerno.value; var Field2 = document.form1.mobileno.value; if (Field1 == "") { alert ( "Please fill in your consumer no.." ); valid = false; } if (Field2 == "") { alert ( "Please fill in your mobile no.." ); valid = false; } for (i=0; i < Field1.length; i++) { if (Numbers.indexOf(Field1.charAt(i),0) == -1) { alert ( "Numbers only in Consumer Number." ); valid = false; break; } } for (i=0; i < Field2.length; i++) { if (Numbers.indexOf(Field2.charAt(i),0) == -1) { alert ( "Numbers only in Mobile Number." ); valid = false; break; } } return valid; } </script> Enjoy !
actually im new here and in js. i want one more function there like the numeric value should be equal to 11digit as mobile number. i think that code should be like HTML: <script language="JavaScript"> function onlyNumbers() { valid=true; var Numbers = '0123456789'; var Field1 = document.Form1.consumerno.value; var Field2 = document.Form1.mobile.value; if (Field1 == "") { alert ( "Please fill in your consumer no.." ); valid = false; } if (Field2 == "") { alert ( "Please fill in your mobile no.." ); valid = false; } for (i=0; i < Field1.length; i++) { if (Numbers.indexOf(Field1.charAt(i),0) == -1) { alert ( "Numbers only in Consumer Number." ); valid = false; break; } } for (i=0; i < Field2.length; i++) { if (Numbers.indexOf(Field2.charAt(i),0) == -1) { alert ( "Numbers only in Mobile Number." ); valid = false; break; } } return valid; } function validateForm(Form1) { Form1.onsubmit = function(){ if(Form1.elements['mobile'].value.length<>11) { alert("You must put 11 digit!"); return false; } } </script> or what would be better