Does one validation rule suppress the other validation rule in jquery validation plugin?
I have following code
<html>
<head>
<title>Untitled Document</title>
<script type="text/javascript" src="js/jquery-1-4-2.js"></script>
<script type="text/javascript" src="js/jquery_validate.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#form1").validate({
rules: {
fld1: {
methodtwo: true,
methodone: true
}
},
messages: {
fld1: {
methodtwo: "<br> 2Wrong Project Name.",
methodone: "<br> 1Wrong Project Name."
}
}
});
});
$.validator.addMethod('methodone', function (value) {
return /^[a-zA-Z0-9]+$/.test(value);
}, 'wrong1.');
$.validator.addMethod('methodtwo', function (value) {
return /^[a-zA-Z^0-9]+$/.test(value);
}, 'wrong2.');
</script>
</head>
<body>
<form id="form1"name="form1" action="" method="get">
<table width="80%" border="0">
<tr>
<t开发者_C百科d><input type="text" name="fld1" id="idfld1"></td>
</tr>
<tr>
<td><input type="submit" name="submit" id="idsubmit"></td>
</tr>
</table>
</form>
</body>
</html>
After executing above code I was expecting that if we enter
- alphabets a-zA-z then it will accept the input and it accepted
- any special character it will show error and it showed error
- but when we enter number 0-9 I was expecting that It will show error message of methodtwo. but its accepted it.
Please explain me that Why this happened? As I am checking that what will happen when if two opposite validation rules applied to one field.
Please explain me friends is there any thing wrong with this code or one validation rule suppress the another one?
Thank You!
As Tim correctly points out, the ^
caret is only a metacharacter when it is the first char in a character class.
One thing that can help when writing a regex is to write out, in english (or your native language of choice), exactly what the regex matches. Lets do that with your two regexes:
First regex: /^[a-zA-Z0-9]+$/
means: "At the beginning of the string match one or more characters that can be an alpha or a numeric, then match the end of the string."
Second regex: /^[a-zA-Z^0-9]+$/
means: "At the beginning of the string match one or more characters that can be an alpha, the ^
caret, or a numeric, then match the end of the string."
When you spell it out like this your error is immediately apparent. For the second regex, you simply need to remove the numeric expression from the character class of allowed chars like so:
New regex: /^[a-zA-Z]+$/
means: "At the beginning of the string match one or more alpha characters, then match the end of the string."
The ^
only means "not" when it's the first character inside a character class. In any other position, it means a literal ^
.
So you probably want
/^\D+$/
for method 2. (\D
means "any character except a digit", and that already includes a-zA-Z
.)
However, I'm not sure what you're really trying to do. You can combine all those rules into one single regex (but at present they do contradict each other, so it's a bit puzzling).
精彩评论