Jqueryvalidate custom verification method introduction date verification _ jquery-js tutorial

Source: Internet
Author: User
This article mainly introduces the date verification of the jqueryvalidate custom verification method. If you need a friend, you can refer to it and hope to help you with jquery validate's many verification rules, however, you need to customize verification rules based on specific situations.

Here we will talk about the custom validation of jquery validate.

Jquery validate has a method that allows you to customize verification rules.

Case 1:

The Code is as follows:


// Custom Verification
$. Validator. addMethod ("isPositive", function (value, element ){
Var score =/^ [0-9] * $ /;
Return this. optional (element) | (score. test (value ));
}, "Enter a number greater than 0 ");


You can use addMethod to customize your own verification rules.

This method has three parameters. The first parameter indicates the verification rule name. Here is isPositive, indicating whether it is a positive number.

The second parameter is the real verification subject. It is a function. The first value of the function indicates the value of the form that calls the verification rule. The second element can be used to determine whether it is null, if it is null, this verification rule will not be called.

The third parameter is the returned error message.

How to use it?

In fact, it is the same as the validation rules inherent in jquery validate.

The Code is as follows:



Total score:



As shown above, the method is used in bold. A total of three verification rules are used. One is required, the other is a number, and the other is a custom verification rule.

As follows:

Case 2:

When submitting a form, you often need to verify the date. For example, the end time must be later than the start time.

In this case, you can use jquery validate to customize a verification method for verification.

The method is as follows:

The Code is as follows:


$. Validator. addMethod ("compareDate", function (value, element ){
Var assigntime = $ ("# assigntime"). val ();
Var deadlinetime = $ ("# deadlinetime"). val ();
Var reg = new RegExp ('-', 'G ');
Assigntime = assigntime. replace (reg, '/'); // Regular Expression replacement
Deadlinetime = deadlinetime. replace (reg ,'/');
Assigntime = new Date (parseInt (Date. parse (assigntime), 10 ));
Deadlinetime = new Date (parseInt (Date. parse (deadlinetime), 10 ));
If (assigntime> deadlinetime ){
Return false;
} Else {
Return true;
}
}, "The end date must be later than the start date ");


The red part of the above Code processes the time string in the standard format of 08:09:00,

The replace method is used for processing. This method is used in combination with a regular expression, that is, the reg object in the first line.

After replacement, what if the comparison time? Three processes are required,

1. Convert the standard time to the timestamp through the Date. parse () method.

2. Convert the timestamp to an integer. Make sure that the timestamp is processed by parseInt ("", 10.

3. Convert the timestamp into the Date object new Date ().

After converting to an object, you can compare the time and directly judge whether the end time is earlier than the start time. If the end time is earlier than the start time, an error is prompted.

In this case, compareDate can be verified just like other jquery validate verification rules.

Case 3: ajax Verification

It is also frequently used to verify the existence of the user name in the database.

The Code is as follows:


$. Validator. addMethod ("checkUserExist", function (value, element ){
Var user = value;
$. Ajax ({
Type: "POST ",
Async: false,
Url: "/default/index/ajax/do/ajaxcheckuser ",
Data: "nick =" + user,
Success: function (response ){
If (response ){
Res = false;
} Else {
Res = true;
}
}
});
Return res;
}, "The user name already exists ");


Background verification code:

The Code is as follows:


Case 'ajaxcheckuser ':
$ Nick = trim ($ this-> _ getParam ('Nick '));
If (isset ($ nick )){
$ Where ['lx _ user. nick =? '] = Array ('type' => 1, 'val' => $ nick );
$ AUser = $ daoUser-> getUser ($ where );
If (count ($ aUser)> = 1 ){
Echo TRUE;
} Else {
Echo FALSE;
}
} Else {
Echo FALSE;
}
Break;


If the database exists, true is returned.

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.