Question

I'm using jQuery validation engine for the user registration form on my site and I'm looking for a way to implement an additional control to prevent the usage of special characters, only allowing the use of letters, numbers and the following characters: ,-'. I've tried the following rule, but it won't prevent the usage of: #;" for example. Can someone please tell me where I'm going wrong with the regex?

"noSpecialChars": {
    "regex": /^[0-9a-zA-Z\ \'\-\,]+$/,
    "alertText": "* Text contains invalid characters"
},
Was it helpful?

Solution

/^[0-9a-zA-Z\'\-\,]+$/

this catches only sequences of chars case-insensitive, numbers and ' - , without spaces! If you allow spaces only add \. after the Z. you can test it here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top