Tomato form Validation Class Code modified version _ form effects

Source: Internet
Author: User
Tags border color eval tagname
Once in the classic forum, personal projects in the background to deal with this thing, for simple form verification is very convenient.
Because you don't want the code to get too bloated, there's a lot of less-common functionality that doesn't add
I have not made any changes to the views of Foshan people, why? Because I'm lazy, haha
Today see Omeweb also modified a version, made a lot of changes, changed quite good, thank you.

The source code here:

Remove spaces on both sides of a string
String.prototype.trim = function () {
Return This.replace (/(^\s+) | ( \s+$)/g, "");
}
Detects whether a string is empty
String.prototype.isEmpty = function () {
Return! (/.? [^\s]+/.test (this));
}
Detects whether a value is between two specified values
String.prototype.isBetween = function (val, min, max) {
Return isNaN (val) = = False && val >= min && val <= max;
}
Get 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 validation Regular expression
This.reg = new Object ();
This.reg.english =/^[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.zipcode =/^[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 = ' authentication type not found, cannot perform validation. ';
This.tip.paramError = ' parameter set error, cannot perform validation. ';
this.tip.required = ' is not allowed to be empty. ';
This.tip.english = ' allows only English characters and underscores (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 e-mail 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 cell phone number. ';
This.tip.ip = ' is not a valid IP address. ';
This.tip.zipcode = ' 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 number. ';
Get Control Name
This.getcontrolname = function ()
{
Return This.element.getAttribute (' controlname ') = null
? ' Specify the value of the control '
: This.element.getAttribute (' controlname ');
}
Set focus
This.setfocus = function (ele) {
try {
Ele.focus ();
catch (e) {}
}
Set 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 information
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);
};
Performing string validation
This.validate = function () {
var v = this.element.value;
Verify that Non-null is allowed
var required = This.element.getAttribute (' Required ');
if (required!= null && v.isempty ()) {
This.feedback (' required ');
return false;
}
Verify that the legal format is valid
var dataType = This.element.getAttribute (' DataType ');
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;
}
}
Perform data validation
var confirm = This.element.getAttribute (' Confirm ');
if (confirm!= null) {
try {
var data = eval (' formobj. ' + Confirm + '. Value ');
if (v!= data) {
Alert (' Two input is inconsistent, please re-enter. ');
This.setbordercolor (this.element);
This.setfocus (this.element);
return false;
}
catch (e) {
This.feedback (' Paramerror ');
return false;
}
}
Verify Number 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 between ' + min + '-' + Max + '
The number between. ');
return false;
}
}
Verify 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 an 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 ();
}

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.