I have been working on a form that accepts Twitter parameters such as # and # to populate a Twitter feed.
With Angular.js I had planned to use the built in ng-pattern directive to validate the input before saving, however the validation is acting extremely strangely. It marks a "valid" string as invalid on every 2nd character of the input while typing.
Its quite hard to explain the exact behaviour so heres a Plunker.
For completeness I will add my input field with the strange ng-pattern here:
<input type="text" ng-pattern="/(^|\s)#(\w+)|(^|\s)#(\w+)/g" ng-model="foo" name="foo"/>
It's because of the global matching with the g option, it works if you take it out.
Calling test or exec multiple times involves state:
As with exec (or in combination with it), test called multiple times on the same global regular expression instance will advance past the previous match.
Basically it's trying to move on to another match, but can't find one:
a = /#(\w+)$/g;
> /#(\w+)$/g
a.exec("#test")
> ["#test", "test"]
a.exec("#test")
> null
Related
I have a simple text field in Retool that I would like to validate includes the substring "-demo" So anything like test-demo, jeremy-demo, etc. would be acceptable. I'm sure with extra thought I could prevent stuff after the -demo part too, but one step at a time..
The closest I've gotten is using the pattern \-demo which is validating only when the string is EXACTLY "-demo" no more no less.
Using their own RegEx Tester (here) I've found /\-demo/g to work, but it does not actually work in practice.
please help I have the regex line ^((?![<>=^##]).)*$ which checking not ordinary symbols in an input field, it's ok but I need to add for this line one more condition , my line need to have vs.
For example when we have the name of sport game like this Patriots vs. Tigers
How can I complete my ^((?![<>=^##]).)*$ condition and add rule for checking vs. in line (input field must have vs.) ?
It will be so cool if conditional also check spaces around vs. at left and at right, because for example Patriotsvs.Tigers is not good and need to show error also
I think what you want is
/^[^<>=^##]*?\bvs\.[^<>=^##]*$/
which blacklists the characters [<>=^##] and requires the literal text "vs." somewhere in the string.
That character blacklist is probably insufficient if you're trying to only approve inputs that won't lead to SQL-injection or XSS. Please consider using a stock input filtering/escaping system with this.
You can use look aheads with the start anchor to effectively use multiple conditions. Here is something that should work for you:
^(?=((?![<>=^##]).)*$)(?=.*?\svs\.\s).*$
Will match:
thing vs. another
Patriots vs. Tigers
Won't match:
th%^hg vs. another
thing another
thingvs.another
For validation in an Angular app, I'm trying to ensure the input value ends with the string "ism", so I'm using
ng-pattern="\b\w+(ism\b)"
This isn't triggering $invalid for the input when the expression isn't matched, however, and neither is
ng-pattern="ism$".
I'm getting an error message in the console, which seems to be saying it doesn't like the expression(s) as typed:
Error: [$parse:lexerr]
http://errors.angularjs.org/1.3.15/$parse/lexerrp0=Unexpected
%20nextharacter%20&p1=s%200-0%20%5B%5C%5D&p2=%5Cb%5Cw%2B(ism%5Cb)...
If I can understand you need something like this ng-pattern="/ism$/":
JSFiddle
<form name="myform">
<input type="text" name="test" ng-model="test" ng-pattern="/ism$/" />
<span ng-show="myform.test.$error.pattern">Not valid pattern!</span>
</form>
Thanks Michelem, that was it. To clarify the answer for others, I didn't realize the starting and ending forward slashes are required in the ng-pattern value, that was the problem. I ended up using ng-pattern="/.*ism$/", but "/\b\w+(ism\b)/" works just as well, as does "/ism$/".
The forward slashes aren't needed if one specifies the ng-pattern value as a RegExp object, e.g., ng-pattern="new RegExp('.*ism$')". It's probably better to put that RegExp into a $scope variable, if it's going to be used more than once.
I've been banging my head against the wall and trying to google a solution for several hours with my "problem".
I need a javascript (html5 input) regex pattern, in a registration form which hasn't been submitted yet, which allows normally: ^[\w]+$ but after a submit, if the page which processes the post finds that username is already been taken, takes user back to the registration form (which is now pre-filled with the values he/she typed).
This time that "username" input field should have a pattern which don't allow user to type that same username again, but everything else will do fine.
I've played around on http://regex101.com/#javascript and came up with 100% the opposite I wanted:
^(?=[\w]*)test(?=[\w]*)$
I've been testing my pattern with string:
abctestabc
test
dfea
atest
testa
Regex I'm trying to obtain should match on everything else on my testing string, except for "test" and my pattern matches ONLY for that one.
Second pattern I came up with (which I shortly thought was what I wanted) was:
^(?:([\w]+test[\w]+)|([\w]+test|(test[\w]+)))$
..but didn't take long since I noticed that this only allows user to input:
*test
*test*
test*
..but nothing without "test" included.
First time on form:
<input type="text" name="username" pattern="^[\w]+$" />
Secound time should be:
<input type="text" name="username" pattern="**PATTERN HERE**" value="test" />
So please Stackoverflow, Pimp my regex!
user3548238, do you mean this?
^(?!test$)\w+$
This pattern will allow \w+, like before, but it will not allow "test".
This is accomplished with a lookahead.
Why don't you start pimping here ?
http://www.infotuts.com/live-username-availability-checker-and-password-strength-indicator-with-jquery-and-ajax/
DEMO
I have 3 TextBoxes on the page. How do I setup one validator (and what's kind of validator?) to validate each of TextBoxes on client side?
First textBox must contains only 1-3 number of digits, second - infinite number of digits, third - 0-5 number of digits
It would be better to use different validators for different text boxes.
Use 3 validators for each with display message as * sign.
Then use Validation Summary control to print a single message for all of them, as you want a single message to be displayed for all. Refer this link: MSDN Validation Summary
Check this too: Validation Summary to work with client-side validations
Your expressions would be along the lines of:
[0-9]{1,3}
[0-9]+
[0-9]{0,5}
Updated following further info:
You could combine the values from each text box into a comma separated value and then validate that using [0-9]{0,3},[0-9]+,[0-9]{1,5} expression.
Either use javascript to copy the value into a hidden textbox field with it's own regex validator using above expression, or just perform the validation manually.
e.g. Using jquery
$('#hiddenCombinedField').val($('#field1').val()+','+$('#field2').val()+','+$('#field3').val());
Run this just before you call validate on the form.
See also http://speckyboy.com/2009/12/17/10-useful-jquery-form-validation-techniques-and-tutorials-2/ for some useful reference material.
You could use a CustomValidator but i would prefer using three distinct validators, one for every TextBox.
It's possible to leave the ControlToValidate-property of the CustomValidator empty. On this way you can validate multiple controls at the same time.