jquery Validate Custom Validation method Introduction Date Validation _jquery

Source: Internet
Author: User
Tags timestamp to date

jquery Validate has a lot of validation rules, but more often, you need to customize the validation rules for specific situations.

Here's a little chat about the custom validation of jquery validate.

jquery Validate has a method that allows users to customize validation rules.

Case one:

Copy Code code as follows:

Custom validation
$.validator.addmethod ("Ispositive", function (value,element) {
var score =/^[0-9]*$/;
return this.optional (Element) | | (Score.test (value));
"<font color= ' #E47068 ' > Please enter a number greater than 0 </font>");

Users can customize their own validation rules by Addmethod

This method has three parameters, the first parameter represents the validation rule name, and here is ispositive, indicating whether it is a positive number.

The second parameter is the real validation body, which is a function, the first value of the function that represents the value of the form that invoked the validation rule, and the second element can be used to determine whether it is empty, and the validation rule is not invoked when it is empty.

The third parameter is the error prompt returned.

How to use it specifically?

is actually the same as the validation rules inherent in jquery validate.

Copy Code code as follows:

<tr bgcolor= "#f7f7f7" height= "align=" "Right" >
<TD class= "font14_s Pdr_12 grey_70" > Total score:</td>
<TD class= "font14_s pl40" align= "left" ><input type= "text" id= "Fullscore" name= "Fullscore" style= "Margin-left" : 10px; margin-right:2px "Value=" <!--{$aExams .fullscore}--> "class= required number ispositive input_233"/></ Td>
</tr>

As shown above, the bold place is the method used, a total of three validation rules, one is required, one is a number, the other is a custom validation rule.

The effect chart is as follows:

Case TWO:

When a form is submitted, it is often necessary to validate the date, such as when the end time must be greater than the start time.

This is the time to customize a validation method through jquery validate.

The method is as follows:

Copy Code code 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 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;
}
}, "<font color= ' #E47068 ' > End date must be greater than start date </font>");

The red part of the code above is the processing of the time string, which is treated as a standard format for 2013/12/12 08:09:00,

The method of replace is used when processing, which is finally combined with the regular expression, which is the first line of Reg object.

What if you compare time when you are done with the replacement? To do three processing,

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

2. Converts a timestamp to an integer to ensure that it is handled by parseint ("", 10).

3. Convert the timestamp to date object new dates ().

After the object, you can compare the time size, direct judgment, if the end time is less than the start time, the error prompted.

This time comparedate can be validated just like any other jquery validate validation rules.

Case THREE: Ajax validation

Go to the database to verify that the username exists, which is also used frequently.

Copy Code code 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;
}, "<font color= ' #E47068 ' > username already exists </font>");

Background validation Code:
Copy Code code 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

Returns true if it exists in the database.

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.