So I have this form that has an id input field.
I want to restrict it to alpha numeric with no spaces so I set this up:
self.pattern = /^[a-z0-9]*$/;
Which is used in my view like this:
<input class="form-control" name="id" type="text" ng-model="controller.model.id" ng-disabled="controller.id" ng-pattern="/^[a-z0-9]*$/" ng-trim="" ng-maxlength="10" required />
But I want to modify my pattern to say that there must be at least 1 letter, but it can have 0 digits.
Can someone let me know how to do this?
You may use
ng-pattern="/^\d*[a-z][a-z\d]*$/"
Add i modifier if you need to also match uppercase letters:
ng-pattern="/^\d*[a-z][a-z\d]*$/i"
If you need to disallow leading/trailing whitespaces add ng-trim="false".
Pattern details
^ - start of string
\d* - 0+ digits
[a-z] - a letter
[a-z\d]* - 0 or more letters or digits
$ - end of string.
Related
I have an input of type number, the value in this input will be saved in a column of type number(12,2), so I want to set the max length to 12 (12 digits in total,
2 of which are after the decimal point).
I tried as following :
<input type="number" md-maxlength="12" ng-pattern="/^[0-9]{0,10}([,.][0-9]{0,2})?$/" step="0.01" ..>
Whenver I type something it's invalid, this only occurs when I add the md-maxlength="12" which I use to display the max length under the input field.
As you can see in the picture the input is invalid.
How can I solve this ?
Edit:
ng-maxlength instead of md-maxlength resolves this issue but the x/12 label under the input which indicates the max length is not displayed anymore.
Edit 2:
The regex now indicates that only 10 number are allowed before decimal point (comma or dot) and 2 are optional after, you can see that the regex is working just fine here : https://regex101.com/r/fdDLFP/3, the problem is when I add the md-maxlength.
I want to ensure that a password a user enters is at least 7 characters long and has at least one uppercase letter, one lowercase letter, one number, and one symbol.
The code seems to pass through the if loop until the symbol argument where it gets stuck.
puts "Please enter your desired password: "
password = []
pass_clear = ()
while pass_clear == (nil) do
password = gets.to_s.chomp
if password.length < 7
puts "Your password must contain at least seven characters."
elsif password.count("a-z") == password.length
puts "Your password must contain at least one uppercase character."
elsif password.count("A-Z") == password.length
puts "Your password must contain at least one lowercase character."
elsif password.count("!","#","#","$","%","^","&","*","(",")","_","-","+","=") < 1
puts "Your password must contain at least one symbol."
elsif password.count("0","1","2","3","4","5","6","7","8","9") < 1
puts "Your password must contain at least one number."
else
puts "Thank you, your account is complete."
pass_clear == 1
end
end
This is the output:
Please enter your desired password:
frank
Your password must contain at least seven characters.
Please enter your desired password:
frankie
Your password must contain at least one uppercase character.
Please enter your desired password:
Frankie
Your password must contain at least one symbol.
Please enter your desired password:
Frankie$
Your password must contain at least one symbol.
Please enter your desired password:
And it continues looping through the symbol stage regardless of how many symbols there are.
How can I ensure these symbols are recognized so the loop can finish?
You are quoting each of the symbols which is incorrect. You also have to escape the - and ^ characters
password.count("!##$%\\^&*()_\\-+=")
works for me in this example.
You'll also need to use a range for your numbers like:
password.count("0-9")
The - character is used for the ranges like "a-z" so it has to be escaped, the carat ^ is used to negate a count so:
password.count("^a-z")
would return a count of everything that wasn't in the range of a-z.
This can come in handy as you may want to prevent certain characters from being in your password strings. You could do something like:
password.count("^a-zA-Z!##$%\\^&*()_\\-+=0-9)
This would count any other characters outside what you've defined so you would want to get a zero return value to know they didn't use any forbidden characters.
Some further clarification on ranges in count(). The term "range" should not be confused with the Ruby class "Range". The class of Range uses ".." or "..." for the intervening items. In the count() method the range being considered is the ASCII range from the first character's ASCII number to the second character's ASCII number. That's why in my original typo of A-z it was counting ASCII 65 ("A") to ASCII 122 ("z") which happens to include the characters ASCII 92 to 96 which are not letters but \ ] ^ _ `
One option is to use a regex that contains four positive lookaheads, all of which operate from the beginning of the string.
R = /
(?=.*\p{Ll}) # match lowercase letter
(?=.*\p{Lu}) # match uppercase letter
(?=.*\d) # match digit
(?=.*[#{Regexp.escape("!##$%^&*(,)_+=-")}]) # match special char
/x # free-spacing regex def mode
def password_ok?(str)
str.match?(R)
end
password_ok? "aA1#" #=> true
password_ok? "A1#" #=> false
password_ok? "a1#" #=> false
password_ok? "aA#" #=> false
password_ok? "aA1" #=> false
I want to do like this : 3.40 and not more than 3 characters and one dot:
<md-input-container class="md-block">
<label>Marks/CGPA</label>
<input type="text" name="education.cgpa" ng-model="education.cgpa"
ng-pattern="/^[0-9]{0,4}$/">
<div class="input-validation" ng-show="educationSaveForValidate['education.cgpa'].$error.pattern">
Insert valid CGPA
</div>
</md-input-container>
How can I allow only 3 digits and one dot in Regular Expression?
You may use a single regex like
ng-pattern="/^(?!.{5})\d*\.?\d+$/"
or - to allow an empty string:
ng-pattern="/^(?!.{5})\d*\.?\d*$/"
You may also move the length check out of the regex:
ng-pattern="/^\d*\.\d*$/" ng-maxlength="4"
Details
^ - start of string
(?!.{5}) - a negative lookahead that fails the match if there are any 5 chars in the input string
\d* - 0+ digits
\.? - an optional .
\d* - zero or more digits (if \d+ is used, then 1 or more digits)
$ - end of string.
To disallow any leading/trailing spaces, add ng-trim="false".
https://regex101.com/r/kF0hJ5/17
Check this link above, I hope it'll help you. Sorry for commenting link here. Doing so as I have less repo.
i have solved my problem this way......
ng-pattern="/^\d\.\d{0,2}$/"
<input ng-pattern="/(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^a-zA-Z0-9])/" name="text" type="text" ng-model="formData">
From this I can check minimum 1 lowercase character, 1 uppercase character, 1 integer and 1 special symbol.
But I want to validate 2 lowercase characters, dynamically minimum numbers will change. So I have to write common validation.
How can I do this?
Can anyone help me. Thanks in advance.
use this for 2 lowercase, 2 uppercase, 2 integer, 2 special character
(?=.*[a-z]{1,2})(?=.*[A-Z]{2,2})(?=.*\d{2,2})(?=.*[^a-zA-Z0-9]{2,2})
try it here
I have this pattern like so
<input name="ip" ng-pattern="/^http:\/\/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]{4,5}/" class="form-control full-width text-right" type="text" ng-model="resource.ip"/>
The last part {4,5} is at least 4 and no more than 5. But when I type more than 5 integers at the end the form doesn't change to ng-invalid
This works on http://www.regexr.com/
Any idea why my input is allowing more than 5 characters?
Just add $ to define the match end.
^http:\/\/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]{4,5}$
From the Reference > Archors > end:
Matches the end of the string, or the end of a line if the multiline flag (m) is enabled. This matches a position, not a character.