FormValid0.5 version release, with Ajax custom Validation example _ form effects

Source: Internet
Author: User
Tags eval trim
The latest updates have been modified when the input component name is included []. The focus on the component will appear JS error, only IE is the case, did not find a good solution, I just to exclude this situation, that is, when the component name contains [] does not focus on moving up, Please contact me for any good solution.
and made a demo8--combining jquery to implement AJAX custom validation
Download: formvalid.zip
Copy Code code as follows:

/*
* Copyright (c) 2006-2008 coderhome.net
* All rights reserved.
* Support: Zhi fan (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 to 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 ' Isemail ':
Case ' Isphone ':
Case ' IsMobile ':
Case ' Isidcard ':
Case ' Ismoney ':
Case ' Iszip ':
Case ' isqq ':
Case ' Isint ':
Case ' Isenglish ':
Case ' Ischinese ':
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 Downloads
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.