I’m using jQuery’s validation plugin to validate form text fields.
I’m trying to validate a price field by only allowing currency symbols, letters and numbers but no other special characters.
I have tried this but it’s allowing other special characters such as ; ” :
^[a-zA-Z 0-9 € £]+$
What am I doing wrong?
Thanks!
EDIT:
Code in document.ready function:
jQuery.validator.addMethod("acceptPrice", function(value, element, param) {
return value.match(new RegExp(param + "$"));
});
$("#editForm").validate({
onkeyup: false,
rules: {
priceField: { acceptPrice: "^[a-zA-Z0-9€£]+$" },
}
You should use regex pattern
^[a-zA-Z0-9€£]+$Even your wrong pattern does not allow characters such as
;,"or:. You must have some error in your code, not just in regex pattern.UPDATE [1]
If you want to allow also space character, then use regex pattern
^[ a-zA-Z0-9€£]+$UPDATE [2]
If you want to allow also decimal numbers, you need to allow dot as well:
^[. a-zA-Z0-9€£]+$UPDATE [3]
JavaScript Form Validation
…with script: