Form verification for tomato

Source: Internet
Author: User
Tags border color eval regular expression tagname trim valid
Tip: you can modify some code before running

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Form verification for tomato</title><script language="javascript" type="text/javascript">// Remove the spaces on both sides of the String. prototype. trim = function () {return this. replace (/(^ \ s +) | (\ s + $)/g, "");} // check whether the String is empty. prototype. isEmpty = function () {return! (/.? [^ \ S] + /. test (this);} // check whether the value is between a specified String. prototype. isBetween = function (val, min, max) {return isNaN (val) = false & val> = min & val <= max ;} // obtain the maximum or minimum value String. prototype. getBetweenVal = function (what) {var val = this. split (','); var min = val [0]; var max = val [1] = null? Val [0]: val [1]; if (parseInt (min)> parseInt (max) {min = max; max = val [0];} return what = 'min '? (IsNaN (min )? Null: min): (isNaN (max )? Null: max);} var validator = function (formObj) {this. allTags = formObj. getElementsByTagName ('*'); // string verification regular expression this. reg = new Object (); this. reg. [a-zA-Z0-9 _ \-] + $/; this. reg. chinese =/^ [\ u0391-\ uFFE5] + $/; this. reg. number =/^ [-\ +]? \ D + (\. \ d + )? $/; This. reg. integer =/^ [-\ +]? \ D + $/; this. reg. float =/^ [-\ +]? \ D + (\. \ d + )? $/; This. reg. date =/^ (\ d {4}) (-| \/) (\ d {1, 2}) \ 2 (\ d {1, 2}) $/; this. reg. email =/^ \ w + ([-+.] \ w +) * @ \ w + ([-.] \ w + )*\. \ w + ([-.] \ w +) * $/; this. reg. url =/^ (ht | f) tp (s ?)) \: \ //) [A-zA-Z0-9] + \. [a-zA-Z0-9] + [\/= \? % \-&_~ '@ [\] \': +!] * ([^ <> \ "\"]) * $/; This. reg. phone =/^ (\ d {2, 3} \) | (\ d {3 }\-))? (\ (0 \ d {2, 3} \) | 0 \ d {2, 3 }-)? [1-9] \ d {6, 7} (\-\ d {1, 4 })? $/; This. reg. mobile =/^ (\ d {2, 3} \) | (\ d {3 }\-))? (13 \ d {9}) | (159 \ d {8}) $/; this. reg. ip =/^ (0 | [1-9] \ d? | [0-1] \ d {2} | 2 [0-4] \ d | 25 [0-5]). (0 | [1-9] \ d? | [0-1] \ d {2} | 2 [0-4] \ d | 25 [0-5]). (0 | [1-9] \ d? | [0-1] \ d {2} | 2 [0-4] \ d | 25 [0-5]). (0 | [1-9] \ d? | [0-1] \ d {2} | 2 [0-4] \ d | 25 [0-5]) $ /; this.reg.zip code =/^ [1-9] \ d {5} $/; this. reg. qq =/^ [1-9] \ d {4, 10} $/; this. reg. msn =/^ \ w + ([-+.] \ w +) * @ \ w + ([-.] \ w + )*\. \ w + ([-.] \ w +) * $/; this. reg. idcard =/(^ \ d {15} $) | (^ \ d {17} [0-9Xx] $)/; // error output information this. tip = new Object (); this. tip. unknow = 'unfound verification type, unable to perform verification. '; This. tip. paramError =' The parameter is set incorrectly and cannot be verified. '; This. tip. required =' cannot be blank. '; This. tip. english =' only english characters and underscores are allowed (a-zA-Z0-9 _). '; This. tip. chinese =' only chinese characters are allowed. '; This. tip. number =' is not a valid number. '; This. tip. integer =' is not a valid integer. '; This. tip. float =' is not a valid floating point number. '; This. tip. date =' is not a valid date format. (For example, 2007-06-29) '; this. tip. email =' is not a valid email format. '; This. tip. url =' is not a valid hyperlink format. '; This. tip. phone =' is not a valid phone number. '; This. tip. mobile =' is not a valid mobile phone number. '; This. tip. ip =' is not a valid ip address. '; This.tip.zip code =' is not a valid ZIP code. '; This. tip. qq =' is not a valid qq number. '; This. tip. msn =' is not a valid MSN account. '; This. tip. idcard =' is not a valid ID card number. '; // Obtain the control name this. getControlName = function () {return this. element. getAttribute ('controlname') = null? 'Specify the control value': this. element. getAttribute ('controlname');} // sets the focus this. setFocus = function (ele) {try {ele. focus () ;}catch (e) {}// set the border color this. setBorderColor = function (ele) {var borderColor = ele. currentStyle? Ele. currentStyle. borderColor: document. defaultView. getComputedStyle (ele, null) ['bordercolor']; ele. style. borderColor = '# ff9900'; ele. onkeyup = function () {this. style. borderColor = borderColor; }}// output error feedback this. feedback = function (type) {try {var msg = eval ('This. tip. '+ type) = undefined? Type: this. getControlName () + eval ('This. tip. '+ type);} catch (e) {msg = type;} this. setBorderColor (this. element); alert (msg); this. setFocus (this. element) ;}; // execute string verification this. validate = function () {var v = this. element. value; // verify whether non-empty var required = this. element. getAttribute ('required'); if (required! = Null & v. isEmpty () {this. feedback ('required'); return false;} // verify whether the format var dataType = this. element. getAttribute ('ype Ype '); if (! V. isEmpty () & dataType! = Null & dataType. toLowerCase ()! = 'Password') {dataType = dataType. toLowerCase (); try {if (! (Eval ('This. reg. '+ dataType )). test (v) {this. feedback (dataType); return false ;}} catch (e) {this. feedback ('unknow'); return false ;}// run the data verification var confirm = this. element. getAttribute ('confirm'); if (confirm! = Null) {try {var data = eval ('formobj. '+ confirm +'. Value'); if (v! = Data) {alert ('the two inputs are inconsistent. Please enter them again. '); This. setBorderColor (this. element); this. setFocus (this. element); return false ;}} catch (e) {this. feedback ('paramerror '); return false ;}// verify the numeric size var dataBetween = this. element. getAttribute ('databetween'); if (! V. isEmpty () & dataBetween! = Null) {var min = dataBetween. getBetweenVal ('min'); var max = dataBetween. getBetweenVal ('Max '); if (min = null | max = null) {this. feedback ('paramerror '); return false;} if (! V. isBetween (v. trim (), min, max) {this. feedback (this. getControlName () + 'must be a number between' + min + '-' + max +. '); Return false ;}}// verification character length var dataLength = this. element. getAttribute ('datalength'); if (! V. isEmpty () & dataLength! = Null) {var min = dataLength. getBetweenVal ('min'); var max = dataLength. getBetweenVal ('Max '); if (min = null | max = null) {this. feedback ('paramerror '); return false;} if (! V. isBetween (v. trim (). length, min, max) {this. feedback (this. getControlName () + 'must be' + min + '-' + max + 'characters. '); Return false ;}} return true ;}; // perform the initialization operation this. init = function () {for (var I = 0; I<this.allTags.length; i++) { if (this.allTags[i].tagName.toUpperCase() == 'INPUT' || this.allTags[i].tagName.toUpperCase() == 'SELECT' || this.allTags[i].tagName.toUpperCase() == 'TEXTAREA') { this.element = allTags[i]; if (!this.validate()) return false; } } }; return this.init();}</script></head><body><form id="form1" name="form1" method="post" action="" onsubmit="return validator(this);"> <table width="600" border="0" cellspacing="0" cellpadding="2" > <tr> <td>Password</td> <td>Password</td> <td><input type="text" name="password" controlName="密码" dataType="password" /></td> </tr> <tr> <td>Confirm password</td> <td>Confirm</td> <td><input type="text" name="confirm" controlName="密码确认" confirm="password" /></td> </tr> <tr> <td width="57">English</td> <td width="60">English</td> <td width="471"><input type="text" name="english" controlName="英文" dataType="english" dataLength="4,10" />4 ~ 10 characters</td> </tr> <tr> <td>Chinese</td> <td>Chinese</td> <td><input type="text" name="chinese" required="required" controlName="中文" />Not empty</td> </tr> <tr> <td>Number</td> <td>Number</td> <td><input type="text" name="number" controlName="数字" dataBetween="10,100" />Greater than 10 and less than 100</td> </tr> <tr> <td>Integer</td> <td>Integer</td> <td><input type="text" name="integer" controlName="整数" dataType="integer" /></td> </tr> <tr> <td>Floating point number</td> <td>Float</td> <td><input type="text" name="float" controlName="浮点数" dataType="float" /></td> </tr> <tr> <td>Date</td> <td>Date</td> <td><input type="text" name="date" controlName="日期" dataType="date" /></td> </tr> <tr> <td>Email</td> <td>Email</td> <td><input type="text" name="email" controlName="邮件" dataType="email" /></td> </tr> <tr> <td>URL</td> <td>Url</td> <td><input type="text" name="url" controlName="网址" dataType="url" /></td> </tr> <tr> <td>Phone Number</td> <td>Phone</td> <td><input type="text" name="phone" controlName="电话" dataType="phone" /></td> </tr> <tr> <td>Mobile phone</td> <td>Mobile</td> <td><input type="text" name="mobile" controlName="手机" dataType="mobile" /></td> </tr> <tr> <td>IP address</td> <td>Ip</td> <td><input type="text" name="ip" controlName="IP地址" dataType="ip" /></td> </tr> <tr> <td>Zip Code</td> <td>Zipcode</td> <td><input type="text" name="zipcode" controlName="邮编" dataType="zipcode" /></td> </tr> <tr> <td>QQ number</td> <td>Qq</td> <td><input type="text" name="qq" controlName="QQ号码" dataType="ip" /></td> </tr> <tr> <td>MSN</td> <td>Msn</td> <td><input type="text" name="msn" controlName="MSN" dataType="msn" /></td> </tr> <tr> <td>ID card</td> <td>Idcard</td> <td><input type="text" name="idcard" controlName="身份证" dataType="idcard" /></td> </tr> <tr> <td colspan="3" align="left"><input type="submit" name="Submit" value=" 提交 " /></td> </tr> </table></form></body></html>
Tip: you can modify some code before running
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.