JavaScript Form Validation instance Verify that the form item is empty _javascript tips

Source: Internet
Author: User

Form validation is essential for almost every website that needs to be registered or logged in. Some of the validation is very complex, it can be said that a variety of requirements for you, but this chapter only describes the simplest form of validation, is to determine whether the empty, some of the requirements of the lower Web site has been satisfied with the need.

The code is as follows:

 <! DOCTYPE html>  
 

The above code, when clicked the Submit button, can carry on the simple form verification, if the form item is empty, then will pop-up the prompt, and will focus in the current form item, the code is relatively simple, here the distance does not have many introduction, may refer to the related reading.

Here is a look at the following JS validation form instance code:

Gspan.html

 

check.js

function Gspan (cobj) {//Get the span label after the form to display the hint information if (cobj.nextSibling.nodeName!= ' span ') {Gspan (cobj.nextsibling); 
else {return cobj.nextsibling;}} Check the form obj "Form object", info "prompt" fun "handle function" click "needs to be clicked, the commit need to trigger" function check (obj, info, fun, click) {var sp = Gspan (ob 
j); 
Obj.onfocus = function () {sp.innerhtml = info;
Sp.classname = ' Stats '; } Obj.onblur = function () {if (Fun (This.value)) {sp.innerhtml = "input correct!"
";
Sp.classname = "Stats";
else {sp.innerhtml = info; sp.classname = "Stats";}}
if (click = = ' click ') {Obj.onblur ();}} onload = regs; Page load Execution function regs (click) {var stat = true;//return status, use username = document.getelementsbyname (' username ') when submitting data []; pass
Word = document.getelementsbyname (' password ') [];
Chkpass = Document.getelementsbyname (' Chkpass ') [];
email = document.getelementsbyname (' email ') []; Check (username, "length of username in-between", function (val) {if Val.match (/^\s+$/) && val.length >= && Val.length & lt;=) {return true;} else {stat = false; retUrn false;
}, click); Check (password, "Password must be between-bit", function (val) {if Val.match (/^\s+$/) && val.length >= && val.length
<=) {return true;} else {stat = false; return false;}}, click); Check (Chkpass, "Make sure the password is consistent with the above, the rules are the same", function (val) {if (Val.match (/^\s+$/) && val.length >= &&
Val.length <= && val = = Password.value) {return true;} else {stat = false; return false;}, click); 
Check (email, "Enter by mailbox Rule", function (val) {if (Val.match (/\w+@\w+\.\w/)) {return true;} else {stat = false; return false;}
}, click);
return stat; }

Related Article

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.