-
Notifications
You must be signed in to change notification settings - Fork 58
Regular Expressions (Regex)
##DEPRECATED
This implementation of using regex: ... :regex
has been deprecated but the code is still in place, I just don't support it anymore as the new implementation is much better. So please use the newer implementation of Regular Expression Pattern by writing pattern=
.
#####Previous implementation
From the example displayed, I introduce the custom regular expression, there is no limitation on regex itself and you can even use the pipe " | "
within it and not being scared of interfering with the other validation filters BUT you have to follow a specific pattern (a writing pattern that is), and if you don't, well it will simply fail. Let's explain how it works...
Regex validation is divided in 4 specific parts (Step #1-4).
Let's use the previous Examples #6 and extract the information out of it to see how it works. Step #1-4 are for explanation only, at the end we show the full regex (make sure there is no spaces).
-
Start & end the filter with the following
regex: :regex
which tells the directive where to extract it. -
Custom error message
YYWW
(what do we want to display to the user, it will be appended to INVALID_PATTERN, please refer to the translation file. For example in English it will display the following:Must be following this format: YYWW
) -
Followed by a separator which basically says... after
:=
separator comes the regex pattern -
Custom regex pattern
^(0[9]|1[0-9]|2[0-9]|3[0-9])(5[0-2]|[0-4][0-9])$
Final code (without spaces):
regex:YYWW:=^(0[9]|1[0-9]|2[0-9]|3[0-9])(5[0-2]|[0-4][0-9])$:regex
###Important note on Regexp
Angular-Validation
uses in the background RegExp
constructor with the first argument defined as a string and there is a very important information to know about this and as mentioned from the great documentation of Mozilla MDN - RegExp Wiki
When using the constructor function, the normal string escape rules (preceding special characters with
\
when included in a string) are necessary.
######Example
// pattern accepting 2 words separated by a space (example a person full name)
var pattern = /(\w+)\s(\w+)/;
// converting the pattern to a string and escaping it will be
var pattern = "(\\w+)\\s(\\w+)";
var re = new RegExp(pattern);
######Test your RegExp before adding it to Angular-Validation To be sure of your regular expression, you could test it prior to add it as a validation
// pattern accepting 2 words separated by a space (example a person full name)
var regex = new RegExp("^(\\w+)\\s(\\w+)$");
var result1 = regex.test("John Smith"); // will return True
var result2 = regex.test("JohnSmith"); // will return False
######Add it as a validation Alright we tested it, it works so let's use it
<input type="text" name="fullname"
validation="regex:Firstname Lastname:=^(\\w+)\\s(\\w+)$:regex|required" />
Contents
- Angular-Validation Wiki
- Installation
- Demo
- Code Samples
- Functionalities
- Custom Validations
- Properties & Options
- Validators
- Tests
- Misc