Conversation
|
I should have checked to see if the element has a pattern attribute.
|
| } | ||
|
|
||
| let r = /^\+?[0-9\-\s]+$/; | ||
| let r = element.pattern ? new RegExp(element.pattern) : /^\+?[0-9\-\s]+$/; |
There was a problem hiding this comment.
Maybe it should be:
| let r = element.pattern ? new RegExp(element.pattern) : /^\+?[0-9\-\s]+$/; | |
| let pattern = element.getAttribute('pattern'); | |
| let r = pattern ? new RegExp(pattern) : /^\+?[0-9\-\s]+$/; |
There was a problem hiding this comment.
But why is it trying to get the pattern attribute from a select?
Property 'pattern' does not exist on type 'HTMLSelectElement'.
Because element: ValidatableElement and only HTMLInputElement has pattern:
aspnet-client-validation/src/index.ts
Line 32 in 061c242
The getAttribute solution works, but you can make TS happy with either:
| let r = element.pattern ? new RegExp(element.pattern) : /^\+?[0-9\-\s]+$/; | |
| let r = 'pattern' in element && element.pattern ? new RegExp(element.pattern) : /^\+?[0-9\-\s]+$/; |
or
| let r = element.pattern ? new RegExp(element.pattern) : /^\+?[0-9\-\s]+$/; | |
| let r = element instanceof HTMLInputElement && element.pattern ? new RegExp(element.pattern) : /^\+?[0-9\-\s]+$/; |
While we're in the area, it could be handy to assign/use the default pattern as something like static phonePattern to allow overriding everywhere by assigning MvcValidationProviders.phonePattern.
|
@mmsaffari Hello! Would you be able to update this? |
The RegExp used in the original code was too loose for a valid Phone number and on the other hand, phone numbers have somewhat different patterns around the world. According to Mozilla one should be able to use the
patternattribute on an<input type="tel" />element to indicate the RegExp to be used to validate the phone number.This PR takes the
patternattribute into account.