html - how to validate form values using external javascript -
here have created 2 fields 1 first name , last name. how can access these fields in external javascript file , validate them.
<html> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> <title>validation</title> </head> <body> <form action="something" method="post"> firstname:<input type="text" name="fname"/> lastname:<input type="text" name="lname"/> <input type="submit" value="submit"> </form> </body> </html>
i want validate both first , last name using external javascript file. how can take values external javascript file , validate. please give me suggestions.
thanks in advance.**
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> <title>validation</title> <script type="text/javascript" src="javascript.js"></script> </head> <body> <form name="myform" action="something" onsubmit="return validateform()" method="post"> firstname:<input type="text" name="fname"/> lastname:<input type="text" name="lname"/> <input type="submit" value="submit"> </form> </body> </html> , java script file is(save javascript.js) function validateform() { var x=document.forms["myform"]["fname"].value; if(x==null || x=="" ) { alert("name can't left blank"); return false; } var y=document.forms["myform"]["lname"].value; if(y==null || y=="") { alert("last name mandatory"); return false; } else { return true; } }
Comments
Post a Comment