JS Form validation function

Source: Internet
Author: User
Tags eval

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 (parsefloat (fstobj.value) <= parsefloat (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 (parsefloat (fstobj.value) + inputobj.getattribute (' operate ') + parsefloat (sndobj.value))) {
return false;
}
}
return true;
}

This.limit = function (inputobj) {
var len = inputObj.value.length;
if (len) {
var MINV = parseint (Inputobj.getattribute (' min '));
var maxv = parseint (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 = parseint (Inputobj.getattribute (' min '));
var maxv = parseint (Inputobj.getattribute (' Max '));
MINV = MINV | | 0;
MAXV = MAXV | | Number.MAX_VALUE;

Return Minv <= Val && val <= maxv;
}
return true;
}

this.requirechecked = function (inputobj,formelements) {
var MINV = parseint (Inputobj.getattribute (' min '));
var maxv = parseint (Inputobj.getattribute (' Max '));
var arrayname = null;
var pos = inputObj.name.indexOf (' [');
if (pos!=-1)
Arrayname = InputObj.name.substr (0,pos);
MINV = MINV | | 1;
MINV = MINV | | 0;

MAXV = MAXV | | Number.MAX_VALUE;

var checked = 0;
if (!arrayname) {
var groups = Document.getelementsbyname (inputobj.name);
for (Var i=0;i<groups.length;i++) {
if (groups[i].checked) checked++;
}

} else {
for (Var i=0;i<formelements.length;i++) {
var e = formelements[i];
if (e.checked==true && e.type== ' checkbox ')
&& e.name.substring (0,arrayname.length) ==arrayname) {
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.istelephone = function (inputobj) {
Inputobj.value = InputObj.value.trim ();
if (Inputobj.value = = "") {
return true;
} else {
if (! RegExps.isMobile.test (inputobj.value) &&! RegExps.isPhone.test (Inputobj.value)) {
return false;
}
}
return true;
}
This.checkreg = function (Inputobj, Reg, msg) {
Inputobj.value = InputObj.value.trim ();

if (Inputobj.value = = "") {
return true;
} else {
Return Reg.test (Inputobj.value);
}
}

    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== ' text ' | | | | | frt.type== ' password ') {
      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);
Formvalid.allname = new Array ();
for (var i=0; i<formelements.length;i++) {
if (formelements[i].disabled==true) continue;
var msgs = Fvcheck (formelements[i],fv,formelements);
if (msgs.length>0) {
For (n in msgs) {
Fv.adderrormsg (Formelements[i].name,msgs[n]);
}
}
}
return fv.passed ();
}
function Fvcheck (e,fv,formelements) {
var validtype = e.getattribute (' valid ');
var errormsg = E.getattribute (' errmsg ');
if (!errormsg) {
ErrorMsg = ';
}
if (validtype==null) return [];
Fv.addallname (e.name);
var VTS = validtype.split (' | ');
var ems = errormsg.split (' | ');
var r = [];
for (Var j=0 j<vts.length; J + +) {
var curvalidtype = vts[j];
var curerrormsg = ems[j];
var Validresult;
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 ':
Validresult = Fv.checkreg (e,regexps[curvalidtype],curerrormsg);
Break
Case ' regexp ':
Validresult = Fv.checkreg (e,new RegExp (E.getattribute (' RegExp '), "G"), curerrormsg);
Break
Case ' Custom ':
Validresult = eval (e.getattribute (' custom ') + ' (e,formelements) ');
Break
Default:
Validresult = eval (' FV. ') +curvalidtype+ ' (e,formelements) ');
Break
}
if (!validresult) R.push (curerrormsg);
}
return R;
}
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}-))? ((0d{2,3}) |0d{2,3}-)? [1-9]d{6,7} (-d{1,4})? $/;
Regexps.ismobile =/^ ((d{2,3}) | ( d{3}-))? (13|15|18) 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[s]?:/ /[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}sd{1,2}:d {1,2}:d {1,2}$/;

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.