For my xpages/jsf application I have setup for an xe:input control the following validation:
<xp:this.validators>
<xp:validateConstraint
regex="${application.regex_email}"
message="please follow the rules">
</xp:validateConstraint>
<xp:validateRequired
message="follow the rules">
</xp:validateRequired>
</xp:this.validators>
The regular expression I use:
regex_email=^([w-]+(?:.[w-]+)*)@(acme.org|acme.com)
(so I allow only email addresses from acme.org and acme.com domains, case sensitive).
This works fine when sending the data to the server but I want to apply something similar to validation on the client-side.
I guess in the onkeyup event a similar check occurs so only the allowed domains in lowercase characters are allowed, so as soon the user would write ‘john.doe@A’ it would be allowed BUT transformed to lowercase (john.doe@a
) and when writing john.doe@s
it would starts alerting the user that the domain is not acceptable.
But I do not know how. Can anyone explain how to?
3
Answers
const regex_email = /^([w-]+(?:.[w-]+)*)@(acme.org|acme.com)$/;
Use the
pattern
attribute of input. It makes it so the form cannot be submitted until the regex in this field matches (see my example below).This is better than a key listener, because even if you try to block certain key strokes, the user can still copy/paste an email address in (unless you block this too, which… do you really want to do this to people?)
Note: This attribute was first introducted in the HTML 5 specification.
This will validate while typing