html5 - Validation of text field in HTML using javascript -
i want validate text field "username" of form per below pasted code. validation make username text field mandatory.
the problem facing here text fields not getting validated , onclick of submit button directly moving next page "index.html" . can suggest me how can correct in below code? want validation happen first , then if passing validation, user can move next page.
my code:
<html> <head> <script> //validation username mandatory field function validatecontactform() { var name = document.contactform.userid; if (userid.value == "") { window.alert("please enter name."); name.focus(); return false; } </script> <title> login page </title> </head> <body> <h1 style="font-family:comic sans ms;text-align="center";font-size:30pt; color:#00ff00;> <strong>welcome</strong> </h1> <form name="login"> username: <input type="text" name="userid "/><br/> password: <input type="password " name="pswrd"/><br/><br/><br/><br/> </form> <head> <title>test input</title> <script language="javascript"> function testresults (form) { var testvar = form.submit(); } </script> </head> <body> <form method="post" action="index.html" name="contactform" onsubmit="return validatecontactform();"> <input type="submit" value="submit"> <input type="reset" value="cancel"/> </form> </body> </html>
instead of using custom javascript validate form (which shouldn't relied on validate form can bypassed), can use new html5 required
attribute on textbox make required on client side.
Comments
Post a Comment