Im using the bassistance form validation. I have a couple of fields that require digits only, but that excludes symbols like $,(,),-,*, etc from being allowed and i need these for fields like prices and phone numbers. Anyone know a method or plugin or something to work around this?
UPDATE
Basically, i need a function that says "no text allowed" and allowed symbols and numbers.
Another update
Found a function that doesnt allow text alone. But if there are symbols, it allows text as well. Any way to restrict any text?
jQuery.validator.addMethod(
"symbols",
function(value,element){
var hNum=/[^a-z\s-]/;
var inp=jQuery.trim(element.value).toLowerCase();
if(hNum.test(inp)) { 开发者_运维百科 return true;
}
else return false;
},
"Number and symbols are only allowed."
);
I wrote a little function for it...
function is_digit(value) {
return String(value).search(/^\s*\d+\s*$/) != -1;
}
You can adapt it as you wish, of course. :)
Edit:
Here are other useful reg-exes:
- Is integer:
/^\s*(\+|-)?\d+\s*$/
? - Is decimal-number:
/^\s*(\+|-)?((\d+(\.\d+)?)|(\.\d+))\s*$/
?
According to their documentation, there's already validation methods that can perform validation for: decimal numbers, digits and phone numbers
**EDIT: In that case it sounds like you want to create a custom validation. There is a method called addMethod which adds a custom validation with a call-back function like the one daGrevis created.
Example:
jQuery.validator.addMethod("isdigit", function(value, element) {
return /[\d()-.\$]/.test(value);
}, "Numbers and symbols only allowed"
);
Well, you might try a different approach to validation entirely.
My personal favorite is to use HTML5's validation and make all browsers jive/extend its functionality.
http://ericleads.com/h5validate/
精彩评论