Allow forward slash in ng-pattern regex. - javascript

Should be fairly straight forward. Trying to allow a forward slash in my numeric only ng-pattern for a form field (expiration date).
HTML:
<input type="text" id="expiration-date" name="expirationDate"
data-ng-model="register.expirationDate"
data-ng-pattern="/^[0-9]+$/"
data-ng-minlength="7"
maxlength="7"
placeholder="MM/YYYY"
data-ui-mask="99/9999"
data-ng-required="true">
Trying to allow the forward slash in the model, otherwise the model never updates because it only accept numbers. I have very little knowledge with regex.

Add a forward slash into the character class.
<input type="text" id="expiration-date" name="expirationDate"
data-ng-model="register.expirationDate"
data-ng-pattern="/^[0-9\/]+$/"
data-ng-minlength="7"
maxlength="7"
placeholder="MM/YYYY"
data-ui-mask="99/9999"
data-ng-required="true">

Related

How to make input password require at least one special character (bracket included)

I was wondering how to add in the special character part with brackets like {} [] () and other stuff like " ' - to the unique character I tried below, but for some reason when I add in another of those characters it stops working.
<-- works but does not have any brackets or quotes for special character-->
<form acton = "#">
<input type="password" id="newPass" required
pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*?[0-9])(?=.*?[!##$%^&*+`~=?\|<>/]).{8,}"
<button>submit</button>
</form>
The part above works but does not have quotes or more special character
Code below has bracket but does not work
<-- does not work (if you do not enter special character user will be able to submit but it does not have any brackets -->
<form acton = "#">
<input type="password" id="newPass" required
pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*?[0-9])(?=.*?[~`!##$%^&*()-_=+[]{};:'.,"\|/?><]).{8,}"
<button>submit</button>
</form>
also this did not work (from an answer)
<form acton = "#">
<input type="password" id="pass" required
pattern="(?=.*\d)(?=.*[a-z])(?=.*?[0-9])(?=.*?[~`!##$%\^&*()\-_=+\[\]{};:\'.,\"\\|/?\>\<]).{4,}">
<button>submit</button>
</form>
I am looking for an answer that makes the bottom part (the one with brackets and all of those special character I included) works.
Since it is regex, all of the characters inside [.....] are allowed, so you should add brackets to there. You can do this with the usage of escape character (because brackets has a role in regex). So just add \] and \[ to the characters allowed inside [.....].
try:
<input type="password" name="pw" pattern="(?=.*?[#?!#$%^&*-\]\[])"
By the way.. I would recommend working with regex cheat sheet, it will help you a lot in validation tasks.
As for your EDIT:
The " and ' needed to be escaped. While \" \' won't work you can use \x27 and \x22 instead, these are the hexadecimal representation of " and ' in the ascii table.
try:
<form acton = "#">
<input type="password" id="pass" required pattern = "(?=.*\d)(?=.*
[a-z])(?=.*?[0-9])(?=.*?[~`!##$%\^&*()\-_=+[\]{};:\x27.,\x22\\|/?><]).{4,}">
<button>submit</button>
</form>
You should escape regex (e.g. \]) characters if they need to be matched during pattern search. Also, refer to the existing answer:
Braces and brackets in passwords
The following works for me, hopefully it should work at your end:
(I merely added escaped characters)
<input type="password" id="newPass" required
pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*?[0-9])(?=.*?[!##$%^&*+`~'=?\|\]\[\(\)\-<>/]).{8,}">
You're actually very close. The biggest mistakes are:
You should escape some of the special characters inside the character group and you should have hyphen as the last character (otherwise it's a range).
Here is the RegExp you request:
<input type="password" id="pass" required pattern = "(?=.*\d)(?=.*[a-z])(?=.*?[0-9])(?=.*?[~`!##$%^&*()_=+\[\]{};:&apos;.,"\\|\/?><-]).{4,}">
Update: I forgot to html encode the pattern for use directly in html. Now it should work.

HTML Field Type Input Number Only or Letter Only Without JavaScript

I create a form, where one field should only be filled with numbers.
I want that field can be filled only with numbers since entering input.
Like this example :
How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
I've tried using Regex, but when I try to input is still able to enter letters.
<input type="number" min="2" pattern="^[0-9]" class="andi_input required-entry" name="amount" id="amount" required title='Only Number' />
I want it when input to field and not after click the Submit button and the message appear and inform that the field can only be filled with numbers.
I also try to add validate-number, but the result is the same.
How, without javascript, so that the field can only be filled with numbers or letters?
Whether for this kind of case have to use JavaScript or is there another way without JavaScript?
HTML 5 uses the type="number" so make sure that the browser that you are using is compatible. Check it out in action here.
You should check browser compatibility.
You're right with <input type="number" pattern="^[0-9]" />.
Your regex rule is :
^[0-9] :
^ assert position at start of the string
[0-9] match a single character present in the list below
(0-9 a single character in the range between 0 and 9)
You can check your regex here.
I use to use HTML5 Validation and jQuery one because IE is capricious most of the time.
UPDATE :
Without Javascript it's not possible to check pattern on real time. I suggest you to use a jQuery library like : http://www.jqueryscript.net/form/jQuery-Plugin-For-Formatting-User-Input-with-Specified-Pattern-formatter-js.html.
Here is a OneLiner:
<input type="number" onkeypress="return /[0-9]/i.test(event.key)">
or
<input type="text" onkeypress="return /[a-z]/i.test(event.key)">

Angular text form with numbers only?

So I have an input form in Angular here:
<input ng-model="sc.zip" class="form-control" maxlength="5" type="text" />
I don't want type="numbers" because the form needs to be a plain empty textbox. However, I only want the user to be able to type numbers. Either I need to detect when the input is not a digit, or be able to search through the box to find non-digits when submitting.
Either way, I need to validate that the form is digits only. Any help would be appreciated!
use regex
<input name="title" type="text" ng-model="sc.zip" ng-pattern="/^[0-9]*$/" required/>

Use regex to do validation in g:TextField

I am working on a grails applications and want the ability to do validation on a g:textField. I just want to make sure that numbers are numbers and words are words. I would like to avoid doing it through JS but if that is the only way, then so be it. Here is what I am trying out.
<g:textField type="number" pattern="^(?:[-+]?[1-9]\d*|0)?(?:\.\d+)?\$" class="form-control" name="hours" value="${hours}"/></div>
Any suggestions or directions? Or should I just do it through JS?
So I was able to figure out the correct regex. The issue that I was having was about the escape characters and the way they were being interoperated.
<g:textField type="number" pattern="^[0-9]+\\s*\$|^[0-9]+\\.?[0-9]+\\s*\$" required="true" class="form-control" name="hours" value="${deliverable.estimatedHours}"/>
This will now check the format for an input string and make sure that it only contains numbers.

Mobile Number Masking using Javascript?

I want to use masking for Mobile Number in javascript.In this masking, I want to fix (+91) then user can input mobile number(10 digit number)
<label>Phone Number</label>
<input type="button" value="Turn on Mask" id="maskon" />
<input id="phone" type="text" value="" />
$("#maskon").click(function(){
$("#phone").mask("(+91) 9999-999999");
});
If I am using the given masking then I am able to put any digit in place of 9 instead of it I want to have (+91) as fixed.
Is there any idea to solve this problem?
Thanks in Advance!!
I do not think that there is an option for making first few digits fixed for mask plugin.
However you could also write keyup event for textbox and write a regex that verifies that first three digits are +91. You can use this regex for that. /(\+\d{2})/

Categories

Resources