I am working on an application using MVC 3 and knockoutjs library. Also I am reseraching the knockout validation plugin. I need to validate an input text value for allowed symbols. As far as I can see, there are not native rules for these so I will have to create a custom one. The bad thing is that my knowledge in regular expressions and javascript is very poor. I need to write a custom function that validates a text box input field and does not allow non-english characters. All the other symbols are permitted. I think I will be able to create the custom rule, but I need the regular expression that validates the input field. Any help with a working example will be greatly appreciated. Thank You!
Share
and
seems contradicting to me but I’ll try to help 🙂
First you should consider using Ajax for validation since server-side languages usually have much better support for Unicode. If it’s not an option then first add XRegExp library with Unicode plugin to your page.
To create a Unicode-aware regular expression use the following form:
Here we say that we accept zero or several (
... *) symbols and each symbol is either ([ ... ]) non-letter (\\p{^Letter}) or letter from basic Latin alphabet (\\p{Latin}).Now your custom validation rule would look like this:
and you would use it in your code like this:
Edit: Removed
|from regexp. Thanks, @slevithan. It wasn’t an error per se since\\p{^Letter}already included|symbol and, thus, the regular expression would still work. However, it was quite misleading.