JavaScript Form, Number and Character Value Validation
Free Web development courses with real-time projects Start Now!!
Program 1
<html> <head><title>Student Registration Form</title> <script lang="javascript"> function validate() { var sid,sname,spass,scpass,sphy,schem,smath sid=document.getElementById("STID").value sname=document.getElementById("STNAME").value spass=document.getElementById("STPASS").value scpass=document.getElementById("STCPASS").value sphy=document.getElementById("STPHY").value schem=document.getElementById("STCHEM").value smath=document.getElementById("STMATH").value if(sid=="") { alert("Student id can't be left blank") document.getElementById("STID").focus() return false } if(sid!="") { var valid="0123456789" for(i=0;i<sid.length;i++) { ch=sid.charAt(i) if(valid.indexOf(ch)==-1) { alert("Only Number allowed in Id... ") document.getElementById("STID").focus() document.getElementById("STID").select() return false } } } if(sname=="") { alert("Student Name can't be left blank") document.getElementById("STNAME").focus() return false } if(sname!="") { var asc for(i=0;i<sname.length;i++) { asc=sname.charCodeAt(i) if(!((asc>=65&&asc<=97)||(asc>=98&&asc<=122)||(asc==32))) { alert("Only Character allowed in name .....") document.getElementById("STNAME").focus() document.getElementById("STNAME").select() return false } } } if(spass=="") { alert("Password can't be left blank") document.getElementById("STPASS").focus() return false } if(scpass=="") { alert("Confirm password can't be left blank") document.getElementById("STCPASS").focus() return false } if(spass!=scpass) { alert("Password and confirm password should be same") document.getElementById("STCPASS").focus() document.getElementById("STCPASS").select() return false } if(sphy=="") { alert("Physics can't be left blank") document.getElementById("STPHY").focus() return false } if(sphy<0 || sphy>100) { alert("Invalid physics marks ") document.getElementById("STPHY").focus() return false } if(sphy!="") { var valid="0123456789" for(i=0;i<sid.length;i++) { ch=sphy.charAt(i) if(valid.indexOf(ch)==-1) { alert("Only Number allowed in Pyysics Marks... ") document.getElementById("STPHY").focus() document.getElementById("STPHY").select() return false } } } if(schem=="") { alert("Chemistry can't be left blank") document.getElementById("STCHEM").focus() return false } if(schem<0 || schem>100) { alert("Invalid chemistry marks ") document.getElementById("STCHEM").focus() return false } if(smath=="") { alert("Maths can't be left blank") document.getElementById("STMATH").focus() return false } if(smath<0 || smath>100) { alert("Invalid maths marks ") document.getElementById("STMATH").focus() return false } } </script> </head> <body bgcolor="cyan"> <center> <h3><font size="10" color="red">Employee Registration</font></h3> <form method="post" action="Fact.html"> <table border="1"> <tr> <th>Student Id:</th> <td><input type=text id="STID" name="txtId"></td> </tr> <tr> <th>Student Name:</th> <td><input type=text id="STNAME" name="txtNAME"></td> </tr> <tr> <th>Password:</th> <td><input type=password maxlength="8" id="STPASS" name="txtpass"></td> </tr> <tr> <th>Confirm Password:</th> <td><input type=password maxlength="8" id="STCPASS" name="txtcpass"></td> </tr> <tr> <th>Physics:</th> <td><input type=text id="STPHY" name="txtphy"></td> </tr> <tr> <th>Chemistry:</th> <td><input type=text id="STCHEM" name="txtchem"></td> </tr> <tr> <th>Maths:</th> <td><input type=text id="STMATH" name="txtmath"></td> </tr> <tr> <td></td> <td> <input type=submit value="Submit" onclick=" return validate()"> <input type=reset value="Reset"> </td> </tr> </form> </table> </center> </body> </html>
If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google