I need a regex expression which will allow following types of input in javascript :-
1,-,2
1,2,-
-,1,2
-
1,2
1
I need it for validation in my website which will allow only above set of values, Please not there could be only one hypen(-) in any case.
1 or 2 in above case can be any number.
Think this is what you need:
^([\d-]+(,[\d-]+)*)?$
Tested via https://regex101.com/
Related
I'm using the Html5 pattern to validate my inputs on forms. I need to make sure an input has the following rules:
A maximum and minimum of 8 characters
The first 3 must be the specific letters wrx (lowercase)
The last 5 must be numbers.
Eg. wrx12345
Can I even do this with pattern or do I need to use JavaScript?
I believe the regex pattern you are looking for is /^wrx[0-9]{5}$/. A visual representation of this here:
And implemented in html:
<input name="example" pattern="^wrx[0-9]{5}$">
You can use regex in Javascript with this regex:
"/[wrz][\d]{5}/g".
To test the minimum = maximum length = 8, you can just test it in javascript.
If the length egual 8, use the regex
Else, show error
I think this could work
You don't need Javascript to do this.
The pattern attribute uses regular expressions so you can use something like this: ^wrx[0-9]{5}$
The ^ and $ indicates the start and end of the string. Then 'wrx' has to be matched exactly and [0-9]{5} looks for 5 number bewteen 0-9.
You can use something like RegExr to test your patterns.
I am looking to validate an input text against the following pattern in JS/JQuery: <some_string>:<some_string>.
Examples:
A110:B120
AB12C:B123
I know this might be too naive, but appreciate any help here.
You could use this:
^[A-Z0-9]+:[A-Z0-9]+$
That will match your examples and any other that has at least 1 character in each side and only has upper case letters and numbers.
You can refer to this answer in order to know how to test a regex against a string.
Try this
"A110:B120 AB12C:B123".match(/(\w+:\w+)/);
MATCH
1. `A110:B120`
2. `AB12C:B123`
or
"A110:B120".match(/(\w+)+:+(\w+)/);
MATCH
1. A110
2. B120
I know it should be simple, and yes i've tested on online regex sites, but i just can't get this to work.
Input string: "w_(number from 1-99),h_(number from 1-99)", e.g: "w_34,h_34"
Expected Output: number replaced, e.g "w_50,h_50"
Test:
'w_34,h_34'.replace('w_[1-9][0-9],h_[1-9][0-9]', 'w_50,h_50')
But it just returns the original string. (w_34,h_34)
You need to use a regular expression to take advantage of the regexp syntax
'w_34,h_34'.replace(/w_[1-9][0-9]?,h_[1-9][0-9]?/, 'w_50,h_50')
This will solve to only 2 digits of numbers. An alternative would be to use the * operator.
'w_34,h_34'.replace(/w_[1-9][0-9]*,h_[1-9][0-9]*/, 'w_50,h_50')
Which would allow n-length numbers to match.
I want to create a regular expression such that it can accept following values:-
100, 100.00, (100),(100.00),$100, $100.00, $(100), $(100.00)
and I am succeed in building the following regular expression:-
/^(\$?(?=\d*(\.\d{1,3})?$))|^(\$?\((?=\d*(\.\d{1,3})?\)$))/
but above regex fails if the value is just $
I want if the value is just $ then it should give not accept it..
But at the same time it should accept blank value also..
Please help me.
Thanks in advance
It seems that your integer part is never optional, so you should make sure the regex makes it mandatory (right now, even the empty string would pass your regex):
/^\$?(?:\d+(?:\.\d{1,3})?$|\(\d+(?:\.\d{1,3})?\)$)/
\d+ means "one or more digits".
I changed my regex as :
^(\$?(?=\d+(\.\d{1,3})?$))|^(\$?\((?=\d+(\.\d{1,3})?\)$))|^((?=\d*(\.\d{1,3})?$))
and its working for me..
I have this RegEx that validates input (in javascript) to make sure user didn't enter more than 1000 characters in a textbox:
^.{0,1000}$
It works ok if you enter text in one line, but once you hit Enter and add new line, it stops matching. How should I change this RegEx to fix that problem?
The problem is that . doesn't match the newline character. I suppose you could use something like this:
^[.\r\n]{0,1000}$
It should work (as long as you're not using m), but do you really need a regular expression here? Why not just use the .length property?
Obligatory jwz quote:
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
Edit: You could use a CustomValidator to check the length instead of using Regex. MSDN has an example available here.
What you wish is this:
/^[\s\S]{0,1000}$/
The reason is that . won't match newlines.
A better way however is to not use regular expressions and just use <text area element>.value.length
If you just want to verify the length of the input wouldn't it be easier to just verify the length of the string?
if (input.length > 1000)
// fail validation