JavaScript Form Validation 示例

使用onsubmit:

HTML:

<form name="myForm" action="demo_form.asp" onsubmit="return validateForm()" method="post">
First name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>

Javascript:

function validateForm()
{
var x=document.forms["myForm"]["fname"].value;
if (x==null || x=="")
  {
  alert("First name must be filled out");
  return false;
  }
}
转载自:

http://www.w3schools.com/js/js_form_validation.asp

你可能感兴趣的:(JavaScript)