FormValid0.5 is released, with examples of ajax custom Verification

Source: Internet
Author: User

The latest update changes. When the input component name contains [], a JS error occurs when the focus is moved to the component. This is the case only for IE, and no good solution is found, I just excluded this situation, that is, when the component name contains [] and the focus is not moved up, please contact me if there is a good solution.
Demo8-ajax user-defined verification with jquery
Download: formvalid.zipCopy codeThe Code is as follows:
/*
* Copyright (c) 2006-2008 coderhome.net
* All rights reserved.
* Support: zhifan (dzjzmj@163.com)
*
* Version: 0.5
*/

Var FormValid = function (frm ){
This. frm = frm;
This. errMsg = new Array ();
This. errName = new Array ();

This. required = function (inputObj ){
If (typeof (inputObj) = "undefined" | inputObj. value. trim () = ""){
Return false;
}
Return true;
}

This. eqaul = function (inputObj, formElements ){
Var fstObj = inputObj;
Var sndObj = formElements [inputObj. getAttribute ('eqaulname')];

If (fstObj! = Null & sndObj! = Null ){
If (fstObj. value! = SndObj. value ){
Return false;
}
}
Return true;
}

This. gt = function (inputObj, formElements ){
Var fstObj = inputObj;
Var sndObj = formElements [inputObj. getAttribute ('eqaulname')];

If (fstObj! = Null & sndObj! = Null & fstObj. value. trim ()! = ''& SndObj. value. trim ()! = ''){
If (fstObj. value <= sndObj. value ){
Return false;
}
}
Return true;
}

This. compare = function (inputObj, formElements ){
Var fstObj = inputObj;
Var sndObj = formElements [inputObj. getAttribute ('objectname')];
If (fstObj! = Null & sndObj! = Null & fstObj. value. trim ()! = ''& SndObj. value. trim ()! = ''){
If (! Eval ('fstobj. value' + inputObj. getAttribute ('operate') + 'sndobj. value ')){
Return false;
}
}
Return true;
}

This. limit = function (inputObj ){
Var len = inputObj. value. length;
If (len ){
Var minv = inputObj. getAttribute ('Min ');
Var maxv = inputObj. getAttribute ('max ');
Minv = minv | 0;
Maxv = maxv | Number. MAX_VALUE;
Return minv <= len & len <= maxv;
}
Return true;
}

This. range = function (inputObj ){
Var val = parseInt (inputObj. value );
If (inputObj. value ){
Var minv = inputObj. getAttribute ('Min ');
Var maxv = inputObj. getAttribute ('max ');
Minv = minv | 0;
Maxv = maxv | Number. MAX_VALUE;

Return minv <= val & val <= maxv;
}
Return true;
}

This. requireChecked = function (inputObj ){
Var minv = inputObj. getAttribute ('Min ');
Var maxv = inputObj. getAttribute ('max ');
Minv = minv | 1;
Maxv = maxv | Number. MAX_VALUE;

Var checked = 0;
Var groups = document. getElementsByName (inputObj. name );

For (var I = 0; I <groups. length; I ++ ){
If (groups [I]. checked) checked ++;

}
Return minv <= checked & checked <= maxv;
}

This. filter = function (inputObj ){
Var value = inputObj. value;
Var allow = inputObj. getAttribute ('allow ');
If (value. trim ()){
Return new RegExp ("^. + \.(? = EXT) (EXT) $ ". replace (/EXT/g, allow. split (/\ s *, \ s */). join ("|"), "gi "). test (value );
}
Return true;
}

This. isNo = function (inputObj ){
Var value = inputObj. value;
Var noValue = inputObj. getAttribute ('novalue ');
Return value! = NoValue;
}
This. checkReg = function (inputObj, reg, msg ){
InputObj. value = inputObj. value. trim ();

If (inputObj. value = ''){
Return;
} Else {
If (! Reg. test (inputObj. value )){
This. addErrorMsg (inputObj. name, msg );
}
}
}

This. passed = function (){
If (this. errMsg. length> 0 ){
FormValid. showError (this. errMsg, this. errName, this. frm. name );
If (this. errName [0]. indexOf ('[') =-1 ){
Frt = document. getElementsByName (this. errName [0]) [0];
If (frt. type! = 'Radio' & frt. type! = 'Checkbox '){
Frt. focus ();
}
}
Return false;
} Else {
Return FormValid. succeed ();
}
}

This. addErrorMsg = function (name, str ){
This. errMsg. push (str );
This. errName. push (name );
}

This. addAllName = function (name ){
FormValid. allName. push (name );
}

}
FormValid. allName = new Array ();
FormValid. showError = function (errMsg ){
Var msg = "";
For (I = 0; I <errMsg. length; I ++ ){
Msg + = "-" + errMsg [I] + "\ n ";
}

Alert (msg );
}
FormValid. succeed = function (){
Return true;
}
Function validator (frm ){
Var formElements = frm. elements;
Var fv = new FormValid (frm );

For (var I = 0; I <formElements. length; I ++ ){
Var validType = formElements [I]. getAttribute ('valid ');
Var errorMsg = formElements [I]. getAttribute ('errmsg ');
If (! ErrorMsg ){
ErrorMsg = '';
}
If (validType = null) continue;
Fv. addAllName (formElements [I]. name );

Var vts = validType. split ('| ');
Var EMS = errorMsg. split ('| ');
For (var j = 0; j <vts. length; j ++ ){
Var curValidType = vts [j];
Var curErrorMsg = EMS [j];

Switch (curValidType ){
Case 'isnumber ':
Case 'ismail ':
Case 'isphone ':
Case 'ismobile ':
Case 'isidcard ':
Case 'ismoney ':
Case 'iszip ':
Case 'isqq ':
Case 'isint ':
Case 'isinc ':
Case 'ischina ':
Case 'isurl ':
Case 'isdate ':
Case 'istime ':
Fv. checkReg (formElements [I], RegExps [curValidType], curErrorMsg );
Break;
Case 'regexp ':
Fv. checkReg (formElements [I], new RegExp (formElements [I]. getAttribute ('regexp '), "g"), curErrorMsg );
Break;
Case 'custom ':
If (! Eval (formElements [I]. getAttribute ('custom') + '(formElements [I], formElements )')){
Fv. addErrorMsg (formElements [I]. name, curErrorMsg );
}
Break;
Default:
If (! Eval ('fv. '+ curValidType +' (formElements [I], formElements )')){
Fv. addErrorMsg (formElements [I]. name, curErrorMsg );
}
Break;
}
}
}
Return fv. passed ();
}
String. prototype. trim = function (){
Return this. replace (/^ \ s * | \ s * $/g ,"");
}
Var RegExps = function (){};
RegExps. isNumber =/^ [-\ +]? \ D + (\. \ d + )? $ /;
RegExps. isEmail =/([\ w-\.] +) @ (\ [0-9] {1, 3 }\. [0-9] {1, 3 }\. [0-9] {1, 3 }\.) | ([\ w-] + \.) +) ([a-zA-Z] {2, 4} | [0-9] {1, 3}) (\]?) /;
RegExps. isPhone =/^ (\ d {2, 3} \) | (\ d {3 }\-))? (\ (0 \ d {2, 3} \) | 0 \ d {2, 3 }-)? [1-9] \ d {6, 7} (\-\ d {1, 4 })? $ /;
RegExps. isMobile =/^ (\ d {2, 3} \) | (\ d {3 }\-))? 13 \ d {9} $ /;
RegExps. isIdCard =/(^ \ d {15} $) | (^ \ d {17} [0-9Xx] $ )/;
RegExps. isMoney =/^ \ d + (\. \ d + )? $ /;
RegExps. isZip =/^ [1-9] \ d {5} $ /;
RegExps. isQQ =/^ [1-9] \ d {4, 10} $ /;
RegExps. isInt =/^ [-\ +]? \ D + $ /;
RegExps. isEnglish =/^ [A-Za-z] + $ /;
RegExps. isChinese =/^ [\ u0391-\ uFFE5] + $ /;
RegExps. isUrl =/^ http: \ // [A-Za-z0-9] + \. [A-Za-z0-9] + [\/= \? % \-&_~ '@ [\] \': +!] * ([^ <> \ "\"]) * $ /;
RegExps. isDate =/^ \ d {4}-\ d {1, 2}-\ d {1, 2} $ /;
RegExps. isTime =/^ \ d {4}-\ d {1, 2}-\ d {1, 2} \ s \ d {1, 2 }:\ d {1, 2 }: \ d {1, 2} $ /;

Local download

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.