I want to validate below input with Alphanumeric and ()._-‘ symbol only.
<input type="text" name="companyName" value="" pattern="[A-Za-z0-9()._-‘]+" title="Alphanumeric with symbols ()._-‘ only" required> </p>
But, when I try the input, only ._‘ can be used, ()- are not working.
You need to escape those characters. They have significance in RegEx and aren't treated as part of the expression. Try the following: [A-Za-z0-9\(\)._\-‘]+
Works fine for me, with the exception of the '-' which probably needs to be first:
[-A-Za-z0-9()._‘]
Try the code below. I don't know why, but the order for the pattern attribute matters.
<form>
<input type="text" name="companyName" value="" pattern="[A-Za-z0-9()_.'-]+" title="Alphanumeric with symbols ()._-‘ only" required>
<input type="submit">
</form>
Related
So I'm trying to make a form for my assignment, and I am using the pattern attribute in html so only certain characters are required to be typed on the text box. But for some reason, the form is saying using that I'm using an incorrect format even though I made my pattern attribute that way.
Here's an example of the code and a picture of the problem.
<form>
<label for="mnumber">Mobile Number:</label>
<input type="text" required id="mnumber" pattern="[0-9]"><br><br>
<input type="submit" value="submit">
</form>
You did write:
pattern="[0-9]"
You are asking for only one number. You just forget to add '+' to accept more than one number. I guess what you are searching for is this:
pattern="[0-9]+"
pattern="[0-9]"
allows for only a single number character to validate (and be submitted). If you want to allow more than one character, here's your options:
One or more numbers:
pattern="[0-9]+"
Zero or more numbers:
pattern="[0-9]*"
One to three numbers:
pattern="[0-9]{1,3}"
you just need to change type="text" to type="number"
Am new on Angular.Need to restrict special characters into input box with angularjs
HTML code:
<input type="text" class="form-control" id="cntry" ng-model="address.countryCode">
Allow only alphabets or digits
You can try this solution by adding pattern:
<input type="text" class="form-control" id="cntry"
ng-model="address.countryCode" pattern="^[a-zA-Z0-9]*$}">
Define a regex in your controller
$scope.regex = /^[^`~!##$%\^&*()_+={}|[\]\\:';"<>?,./1-9]*$/;
And in your html use a ng-pattern directives and pass the above regex as pattern.
<input type="text" class="form-control" id="cntry"
ng-model="address.countryCode" ng-pattern="regex">
For more info visit Restrict Special Characters in HTML & AngularJs
Following JavaScript & regex : How do I check if the string is ASCII only?
how do I use the ASCII-only regex (/^[\x00-\x7F]*$/) in pattern attribute in HTML?
I wish to restrict the user input to only ASCII characters.
<form class="row" name="marketDetails">
<input type="text" class="form-control col-md-12 form-sm" name="name" id="name" placeholder="Name"
value=""
maxlength="50"
pattern="[\x00-\x7F]"
ng-model="vm.marketMetaData.market_name" required/>
</form
Using pattern="[\x00-\x7F]" still lets me use non-ASCII character (e.g ¶) without any error in the form
Thanks.
Use pattern="[\x00-\x7F]+" - that did the trick. thanks!
by Wiktor Stribiżew
I am trying to come up with a valid pattern for an HTML form. I would like to allow *#student.*.edu.vn
This is my code:
<input
type="text"
name="mail"
pattern="([a-z]|[0-9])+(#{1})+(student)+(.)+[a-z]+(.edu.vn)"
/>
It does not work as I would like. What I am doing wrong?
The pattern (regular expression) is slightly wrong as you haven't escaped all your special characters such as the . and have some unnecessary parts within it. Try using the following slightly modified pattern instead:
([a-z]|[0-9])+#student\.[a-z]+\.edu\.vn
<form>
<input type="text" name="mail" pattern="([a-z]|[0-9])+#student\.[a-z]+\.edu\.vn" />
<input type="submit">
</form>
Note: Make sure that you also do validation on the server-side as anyone entering values into a form can remove the pattern to bypass this check.
I came up with this solution which is slightly more easy to read and is less strict (which is a good thing because e-mail addresses can contain a lot more characters than just lowercase alpha-numeric ones. Don't forget to do proper validation on the backend using an e-mail validator.
<form>
<input
type="text"
name="mail"
pattern=".+#student\..+\.edu.vn"
/>
<input type="submit">
</form>
What will be the correct regex pattern for an HTML input which should allow only Letters, digits and #/./+/-/_. No spaces.
You can use this regex for no space:
<form>
<input type="text" pattern="[^' ']+" />
<input type="submit" value="test submit">
</form>
So you can modify it to add your other rules
If characters order and count does not matter you can use this:
^[-a-zA-Z0-9#\.+_]+$
Scratch that, there's actually a regex for that.
<input type="text" pattern="^\S+$">
That would work for MyName, but not for My Name.