Recently, many JavaScript data verifications were performed in the project. to unify the style and port reuse, Jquery plug-ins were encapsulated.
The Code is as follows:
(Function ($ ){
Var defaults = {
BugColor: '# ffcccc', // text box color when data is incorrect
Color: 'white', // the text box color when the data is correct
Type: "alert", // when a data error occurs, the prompt mode alert dialog box is displayed. The text value is span html.
Msg: "Msg", // content prompt when data is incorrect
ResOjId: 'no' // tag assigned when the test method is used # id
};
Function UiProcess (options, rexString, object ){
Var options = $. extend (defaults, options );
Var values = object. val ();
If (rexString. test (values )){
Object.css ("backgroundColor", options. color );
Return true;
} Else {
Object.css ("backgroundColor", options. bugColor );
If (options. type = "alert "){
Alert (options. msg );
}
If (options. type = "text "){
Optional (options.resojid).html (options. msg );
}
Return false;
}
}
// Verify that the ip address meets the format
$. Fn. RegIp = function (options ){
Var rexString =/^ \ d {1} \. {1} \ d {1} \. {1} \ d {1 }/;
Return UiProcess (options, rexString, this)
}
// Verify whether the landline is in the correct format
$. Fn. RegTelPhone = function (options ){
Var rexString =/^ [0-9] + [-]? [0-9] + [-]? [0-9] $ /;
Return UiProcess (options, rexString, this)
}
// Verify that the mobile phone is in the correct format
$. Fn. RegMombilePhone = function (options ){
Var rexString =/(^ 189 \ d {8} $) | (^ 13 \ d {9} $) | (^ 15 \ d {9} $ )/;
Return UiProcess (options, rexString, this)
}
// Verify that Chinese characters are in the correct format
$. Fn. RegCHZN = function (options ){
Var rexString =/[\ u4e00-\ u9fa5]/;
Return UiProcess (options, rexString, this)
}
// Verify that decimal complies with the format
$. Fn. RegDecimal = function (options ){
Var rexString =/^ [0-9] + [.]? [0-9] + $ /;
Return UiProcess (options, rexString, this)
}
// Verify whether decimal retains one decimal in the correct format
$. Fn. RegDecimalSign = function (options ){
Var rexString =/^ [+-]? [0-9] + [.]? [0-9] + $ /;
Return UiProcess (options, rexString, this)
}
// Verify whether the integer retains one decimal point in the format
$. Fn. RegNumber = function (options ){
Var rexString =/^ [0-9] + $ /;
Return UiProcess (options, rexString, this)
}
// Verify that each integer retains one decimal point in the correct format
$. Fn. RegNumberSign = function (options ){
Var rexString =/^ [+-]? [0-9] + $ /;
Return UiProcess (options, rexString, this)
}
// Verify non-null characters
$. Fn. IsEmpty = function (options ){
Var rexString =/(^. + $) | ([\ u4e00-\ u9fa5])/;
Return UiProcess (options, rexString, this)
}
}) (JQuery );
Call:
The Code is as follows: