I am trying to create a regexp to validate an input in HTML, the arithmetic expression which user can enter should be limited to as below
Example
(5>3&8<6)||(98=9||34<40)
(5>=3||8<=6)&(98=9||34<40)
The expression can be (number[<,>,<=,>=,=]number[logical and / logical or ] number ....)
I am using the regexp for input type as pattern="([0-9]*|>|<|=|&)|&|||+".
Thanks.
You can use this (see demo):
\(\d+(?:=|[<>]=?)\d+(?:&|\|\|)\d+(?:=|[<>]=?)\d+\)(?:&|\|\|)\(\d+(?:=|[<>]=?)\d+(&|\|\|)\d+(?:=|[<>]=?)\d+\)
At the moment, in the parentheses we are allowing < and >
Let me know if you need to add or remove any operators. :)
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 have following scenario, I have to validate the form input field whose first character can be either P or B or R, the second character can only be C and after that 6 digits. I was using this pattern:
pattern="^[PBR]C\d{6,6}$"
Now I have to add one more condition to the input field to allow input of the form BRC100101, so now the input can also start from BR, then letter C and then 6 digits. I tried the below pattern, but couldn't quite get what I'm looking for.
pattern="^(P)(B)(R)(BR)C\d{6,6}$"
Least amount of characters and more professional.
([PBR]|BR)C\d{6}
This way is little easier to read/understand, readability goes a long way.
(P|B|R|BR)C\d{6}
I'm not a regular expresssion expert, I usually use regexr.com to help me build and test the expressions I use in my code.
http://regexr.com/
Demo:
http://regexr.com/3ggi6
You can try this way,
([PBR]|BR)C\d{6,6}
Live Demo: https://regex101.com/r/RN86w1/2
You can try
([PBR]|BR)C\d{6}
Demo https://regex101.com/r/SDgguC/1
I'm new to Regular expression validations in JavaScript.
My textbox allows the user to enter a formula which needs to be validated client side.
The formula can be made of following operators:
+
_
*
/
(
)
%
These are the only symbols allowed in my textbox.
I need to validate the formula, which should have a form like CurrentbaseSalary+CurrentMBP(BPI). Something like +BPI-CBI( should not be considered valid.
You can use this regexp :
/^(\([+*%\/\-\d\s]+\)|[+*%\/\-\d\s]+)+$/.test('YOUR STRING')
this regexp check if you are using your allowed symbols and if parenthesis matches, but it cant check if "CurrentbaseSalary" is positive or negative, "CurrentMBP(BPI)" neither.
Some code, or more detailed example could help to find a solution.
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 am trying and failing hard in validating a phone number within jQuery validation. All I want is to allow a number like (01660) 888999. Looking around the net I find a million examples but nothing seems to work. Here is my current effort
$.validator.addMethod("phonenumber", function(value) {
var re = new RegExp("/[\d\s()+-]/g");
return re.test(value);
//return value.match("/[\d\s]*$");
}, "Please enter a valid phone number");
Bergi is correct that the way you are constructing the regular expression is wrong.
Another problem is that you are missing anchors and a +:
var re = /^[\d\s()+-]+$/;
Note though that a regular expression based solution will still allow some inputs that aren't valid phone numbers. You can improve your regular expression in many ways, for example you might want to require that there are at least x digits, for example.
There are many rules for what phone numbers are valid and invalid. It is unlikely you could encode all those rules into a regular expression in a maintainable way, so you could try one of these approaches:
Find a library that is able to validate phone numbers (but possibly not regular expression based).
If you need a regular expression, aim for something that is a close approximation to the rules, but doesn't attempt to handle all the special cases. I would suggest trying to write an expression that accepts all valid phone numbers, but doesn't necessarily reject all invalid phone numbers.
You may also want to consider writing test cases for your solution. The tests will also double as a form of documentation of which inputs you wish to accept and reject.
You need to use either a regex literal or a string literal in the RegExp constructor:
var re = /[\d\s()+-]/g;
// or
var re = new RegExp("[\\d\\s()+-]", "g");
See also Creating a Regular Expression.
Apart from that, you would need to use start- and end-of-string anchors to make sure that the regex matches the whole string, not only a part of it, and some repetition modifier to allow more than one character:
var re = /^[\d\s()+-]+$/g;
Another approach may be:
function(value) {
return /^\d+$/.test(value.replace(/[()\s+-]/g,''));
}
and if you want to check for the length of the number too, say it has to be a string with 10 digits:
function(value) {
return /^\d{10}$/.test(value.replace(/[()\s+-]/g,''));
}