Using JavaScript to implement front-end form verification

Source: Internet
Author: User

Please leave a message if you have any communication.

<! DOCTYPE html>  

<! DOCTYPE html>
Window.onload = function () {
	username =  document.getElementById ("username");
	Password = document.getElementById ("password");
	Password2 = document.getElementById ("Password2");
	email = document.getElementById ("Mail");
	Registerform = document.getElementById ("Registerform");

	Username.addeventlistener ("Blur", function () {
		validateusername ();
	Password.addeventlistener ("Blur", function () {
		validatepassword ();
	},false);
	Password2.addeventlistener ("Blur", function () {		
		validatePassword2 ();
		Issame (PASSWORD,PASSWORD2);
	},false);
	Email.addeventlistener ("Blur", function () {
		validatemail ();
	},false);
	Registerform.addeventlistener ("Submit", function (e) {
		if (Validateusername () &&validatepassword () &&validatepassword2 () &&validatemail ()
			&&issame (Password,password2)) {
			This.submit ();
		} else{
			Preventie (e);
		}
	},false);
})}

Determines whether the form is empty and conforms to a regular expression/directly by name to get Element function Getelementbymymethod (name) {return document.getElementById (name);}
	function Validateusername () {username = Getelementbymymethod ("username"); Return Isemptyandreg (username,/) ([a-z]|[
A-z]) \w{5,12}/);
	function ValidatePassword () {password = Getelementbymymethod ("password");
Return Isemptyandreg (password,/\s{6,12}/);
	function ValidatePassword2 () {password2 = Getelementbymymethod ("Password2");
Return Isemptyandreg (password2,/\s{6,12}/);
	function Validatemail () {email = Getelementbymymethod ("Mail");
Return Isemptyandreg (email,/\w+@\w+\.\w+/);
	function Isemptyandreg (obj,reg) {spanname = obj.id + "Tip";
		if (Obj.value = = "") {settipfailure (spanname);
	return false;
		}else if (reg.test (Obj.value)) {settipsuccess (spanname);
	return true;
		}else{settipfailure (spanname);
	return false;
	}//Determine if 2 passwords are the same function Issame (OBJ1,OBJ2) {if (Obj1.value = = Obj2.value) return true;
		else{Setisnosame ();
	return false; }//settingsThe corresponding prompt information function setisnosame () {//obj is the variable of the corresponding span objspan = document.getElementById ("Password2tip");
Objspan.innertext= "two times password is not the same";
	//Set the appropriate prompt information function settipsuccess (objname) {Objspan = document.getElementById (objname);
Objspan.innerhtml= "<font color= ' green ' >√</font>";
	//Set the appropriate prompt information function settipfailure (objname) {Objspan = document.getElementById (objname);
Objspan.innerhtml= "<font color= ' red ' >x</font>"; }//block browser behavior action function Preventie (e) {if (e && e.preventdefault) {//Now is the Consortium standard E.preventdefault ();//block Browser action}els e{Window.event.returnValue = false;//specifically for IE browser processing}}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.