<! DOCTYPE html>
<meta charset= "UTF-8" >
<title>Title</title>
<script type= "Text/javascript" >
function Validate_email (field,text) {
With (field) {
With is similar to C + + namespace, simply say is the name space, that is, with the {} There is no need to write thisform.xxx, directly write XXX means that is thisform
Here's Value=field.value
apos = value.indexof ("@");//Get where "@" appears
Dotpos = Value.lastindexof ("."); /get "." The location that appears
if (apos<1| | DOTPOS-APOS<2) {//To make a judgment, enter the mailbox must contain the "@" symbol and the dot number "." At the same time "@" can not be the first character of the email address, and "@" must have at least one "." No.
alert (text);//If the condition is true, the cue box pops up
Return false;//here returns false, which means a serious failure
}else{
Alert ("submit succeeded \ n \ nthe Click OK to continue")//if the condition is not set, the Prompt box pops up
Return true;//means validation is successful
}
}
}
function Validate_form (thisform) {//Because onsubmit calls Validate_form (this), here is the form itself, Thisform received this form can also be changed into a form
With (Thisform) {
With is similar to C + + namespace, simply say is the name space, that is, with the {} There is no need to write thisform.xxx, directly write XXX means that is thisform
Here in the Email=thisform.email=form Name=email
if (validate_email (email, "Please enter the correct email address") ==false) {//Call the validate_required parameter is a reference to the name=email tag below Thisform, and the other argument is a string
Email.focus ();//If Validate_email returns FALSE, the focus is set to the email
Return false;//here returns false, which means the validation fails and the form is not committed
}
}
}
</script>
<body>
<form action= "javascript:;" onsubmit= "return Validate_form (This)" method= "POST" ><!--when the form is submitted, the Validate_ is executed first Form (). parameter is the form itself this-->
Email: <input type= "text" id= "email" name= "email" size= "/>"
<input type= "Submit" value= "submitted" id= "Submission"/>
</form>
</body>
JavaScript form Validation-Mailbox verification