-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Slightly Improved phone validator. #115
base: main
Are you sure you want to change the base?
Conversation
I should have checked to see if the element has a pattern attribute.
|
@@ -333,7 +333,7 @@ export class MvcValidationProviders { | |||
return false; | |||
} | |||
|
|||
let r = /^\+?[0-9\-\s]+$/; | |||
let r = element.pattern ? new RegExp(element.pattern) : /^\+?[0-9\-\s]+$/; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
export type ValidatableElement = HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement; |
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
pattern
attribute on an<input type="tel" />
element to indicate the RegExp to be used to validate the phone number.This PR takes the
pattern
attribute into account.