Can anyone suggest a correct regex pattern in angular 2 for accepting numbers and special charterers for angular 2 input field , excepting alphabets
This question has nothing to do with angular, but you can accomplish this with plain javascript. The regexp object's test method returns a boolean showing whether or not your string matches the regular expression.
/^[^a-z]+$/i.test("!##$%") == true // true
/^[^a-z]+$/i.test("Hello") == true // false
In this regex, the initial ^ is the beginning of line anchor and the trailing $ is end of line. The rest of the expression matches 1 or more characters that's not alphabetical. The i flag at the end makes it case-insensitive.
There are a number of nice online regex testing tools. One is https://regex101.com/.
Related
I'm using this regexp:
/[^+][a-z]/.test(str)
I'm trying to ensure that if there are any letters ([a-z]) in a string (str) not proceeded by a plus ([^+]) , a match is found and therefore it will return true.
It mostly works except when there is only one character in the string. For example, a returns false, even though there is no plus sign preceding it.
How can I ensure it works for all strings including one character strings. Thanks!
Add a ^ as an alternative to [^+]:
/(?:^|[^+])[a-z]/.test(str)
^^^^^^^^^^
The (?:^|[^+]) is a non-capturing alternation group matching either the start of the string (with ^) or (|) any char other than + (with [^+]).
This question already has answers here:
AngularJS - Remove leading and trailing whitespace from input-box using regex
(2 answers)
Closed 7 years ago.
I am not very good at regular expressions so maybe this is a simple question, but I am certainly missing something. I use regular expressions to validate specific input from user. The input must be accepted (regex must match) if and only if the input string contains no commas and no whitespaces(in other words, the input must be single word without commas). Except that, it can contain any symbols and the input string can have any length. Now, when I use this regular expression, it matches input, that doesn't contain commas.
/^[^,]*$/
I wanted to add the whitespace part to it, so I made this expression
/^[^,\s]*$/
which behaves in a very weird way. It does what it should except one thing. For some reasons, it matches(and lets in) strings, that end with space (If they end with comma, everything is OK and it doesn't match). I dont wan't it to match strings with trailing whitespaces but I don't know, how to adjust the regular expression to do this. So my questions are - why is this weird thing happening and how to change the regular expression to do what it should.
here is an example:
http://jsbin.com/qoyoyagilo/2/edit?html,js,output
What is even weirder, when I tried my regex on rubular, it didn' t match strings with trailing whitespaces. I am starting to believe, that this has to do something with javascript and not with my particular regex
Angular already trims your strings before validating them and binding to model. Extra whitespace at the beginning and at the end of strings won't even be matched against your regular expression (or any other validator).
You can use ng-trim="false" if you wish to disable this behavior:
<input ng-model="yourmodelvar" ng-trim="false" ng-pattern="[^,\s]*">
Also note that you don't need the ^ and $ chars in your regexp, since validation is performed against the whole string automatically. From the documentation on ng-pattern:
Sets pattern validation error key if the ngModel $viewValue value does
not match a RegExp found by evaluating the Angular expression given in
the attribute value. If the expression evaluates to a RegExp object,
then this is used directly. If the expression evaluates to a string,
then it will be converted to a RegExp after wrapping it in ^ and $
characters. For instance, "abc" will be converted to new
RegExp('^abc$').
References:
https://docs.angularjs.org/api/ng/directive/input (official doc)
How to disable trimming of inputs in AngularJS?
I'm trying to create a function with a regex that can decide if my string value is correct or not. It should be true, if the string begins with lower or uppercase alphabetical characters or underscore. If it begins with any others, the function must return false.
My test input is something like this: ".dasfh"
The expressions, what I tried to use: [_a-zA-Z]..., [:alpha:]..., but both of them returned true.
I tried a bit easier task also:
"Hadfg" where the expression is [a-z]...: returns true
BUT
"hadfg" where the expression is [A-Z]...: returns false
Could anybody help me to understand this behaviour?
You're trying to match the first character in the string to be something in particular, this means you have to tell regex that it has to be the first character in the string.
The regex engine just tries to find any match in the entire string.
All you're telling it with [a-z] is "find me a lowercase character anywhere in the string". This means that:
"Hadfg" will equal true because it can find a, d, f or g as a match.
"HADFG" will equal false because there are no lowercase letters.
the same will happen for "hADFG" when matched with [A-Z] for instance, it will be able to find an A, D, F or G as a match whereas "hadfg" will return false because there is no uppercase character.
What you are looking for here is ^ in your regex, it is a special kind of modifier that indicates "start of line"
So when you apply this to your regex it will look like this: /^[a-z]/.
The regex on the previous line basically says "from the start of the string, is the first character following up a lowercase a-z?"
Try it out and you'll see.
For your solution you'd need /^[_a-zA-Z]/ to check if the first character is an _, a-z or A-Z character.
For reference, you can find cheatsheets within these tools (and test your regexes with it ofcourse!)
Regexr - My personal favorite (Uses your browsers JS regex engine)
Rubular - A Ruby regex tester
Regex101 - A Python / PCRE / PHP / JavaScript
And for a reference or tutorial (I'd recommend reading from start to finish if you want to start understanding regexp and how they work) theres regular-expressions.info.
Regex is never easy and be careful with what you do with it, it's a powerful but sometimes ugly beast to deal with :)
PS
I see you tagged your question as email-validation so I'll add a little bonus regex that validates the minimum requirements for an email address to be absolutely correct, I use this one personally:
.+#.+\..{2,}
which when broken up, looks like this:
.+ - one or more of any character
# - followed by a literal # character
.+ - one or more of any character
\. - followed by a literal . character
.{2,} - two or more of any character
Optionally you could replace {2,} with a + to make it one or more but this would allow a TLD with 1 character.
To see a RFC email-regex at work check this link.
When I look at that regex I basically just want to cry in a corner somewhere, there are definitely things you cannot do in an email address that my regex doesn't address but at least it makes sure it's something that looks like it's e-mailable anyways, if a new user decides to fill in some bull that's not my problem anymore and I wouldn't want to force them to change that 1 character just because the huge regex doesn't agree with it either.
This question already has answers here:
Regular Expression to match only alphabetic characters
(5 answers)
Closed 7 years ago.
I am trying to understand regular expressions, i find it really confusing to understand it. I need to know how to write a regular expression that ONLY matches strings, NO NUMBERS, NO SPECIAL CHARACTERS, just letters from A - Z. I have to say that I tried using \[a-z]\ but it didn't work as i wanted. These are some of the results that I want:
pe<.?ter -----> should return false
ale8 ---------> should return false
?ramon -------> should return false
tho<>?mass----> should return false
peter -----> should return true
alex ------> should return true
ramon -----> should return true
thomas ----> should return true
This should work for you:
var re = /^[a-z]+$/i;
See here
Your regex returns true if it find ONE letter from a to z, only lowercase, in your string.
To make what you want, use /^[a-zA-Z]+$/ or /^[a-z]+$/i.
^ make that your regex start from the begining of the string.
[a-zA-Z] make the regex to find one letter, lowercase OR uppercase (note that you could use instead the i modifier which make the search case insensitive).
+ make the regex to search for one or more times the previous element.
$ make sure that your regex end at the end of the string.
I'm trying to devise a regular expression which will accept decimal number up to 4 digits.
I have successfully done it when a user types it in a text box.
Now, I'm trying to validate text box for paste operation.
For that, I have written a jquery function
function pasteNumber() {
var reNumber = /\d*\.\d{0,4}/;
var theString = window.clipboardData.getData('Text');
if (reNumber.test(theString) == false) {
alert("You are trying to paste an invalid Number!")
return;
}
event.srcElement.value = theString
return;
}
The regular expression which I have used is accepting a value like
44.aaaa
which it should not accept.
Then I tried changing regular expression to
/\d*\.\d{1,4}/
Then, it started to accept values like
44.1aaa
I need help to write a regular expression which will accept values like
4.1
421.11
467.111
438904.1111
0.1
But not
1234.a
489.a
435.aaa
412.1aaaa
1567.11a
In short, there should be no characters.
Any suggestions please?
Thank you
You are only missing the anchors ^ and $
^\d*\.\d{0,4}$
See demo
But, to avoid matching . and 123., you can enhance it as
^\d*\.\d{1,4}$
See update.
As for anchors, they
do not match any character at all. Instead, they match a position
before, after, or between characters. They can be used to anchor the
regex match at a certain position. The caret ^ matches the position
before the first character in the string. Applying ^a to abc matches
a. ^b does not match abc at all, because the b cannot be matched right
after the start of the string, matched by ^.
Similarly, $ matches right after the last character in the string. c$ matches c in abc, while a$ does not match at all.
You just need to add some anchors and group (?:) the entire decimal part and make it optional with a ?:
^\d+(?:\.\d{1,4})?$
^ is for start of string, $ is for end of string
See demo