How to validate algebra expression in javascript - javascript

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.

Related

regexp for arithmetic operation

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. :)

Validate numbers, parenthesis and spaces only in jQuery validation

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,''));
}

restrict characters in form field

I've an HTML field in a form and, using JS and Regex, I must restrict the characters the user can insert in the field. The user of the form can only insert the following characters ( ) * + ^ / X x, and numbers and spaces (when he digits or pastes a different character nothing is written). Is this possible? I need the Regex, I eventually know how to do with JS.
Try this regex
[^\(\)\*+\^/Xx0-9 ]
Here we are trying to find a pattern which contains none of the allowed characters. If you find it this means that the character entered was wrong.
There is no right or wrong answer to this.
Usually it is less expensive to check using the bias of the logic for the particular condition.
If it sounds better saying whats allowed, then use the allowed (positive) character class.
Allowed class: [()*+^/Xx0-9 ], Checks: <space> (-+ /-9 X \^ x
Not allowed class: [^()*+^/Xx0-9 ], Checks: \0-\37 !-' , \- . :-W Y-\] _-w y-\377
Statistically, if %99 of the data enterred were valid, the 'Allowed' class would do less work in that
not every character or range has to be checked.
Where the 'Not-Allowed' class will have to check every range.
In this particular case, the negative class has many more ranges and characters to check, its borderline
more efficient if most of the data were invalid,
With regard to:
[^\(\)\*+\^/Xx0-9 ]
None of those characters need to be escaped inside a set (but it is okay to do so).
If you want to restrict as the user types (this uses jquery methods):
$('#in1').keyup(function (evt) {
var content = $('#in1').val();
$('#in1').val(content.replace(/[^()*+^/Xx0-9]/g, ""));
return true;
});
Where 'in1' is the id of the input.

Regex to validate textbox length

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

How to allow only "numbers","-" and "()" using JavaScript

I have to do phone number validation using JavaScript.
I have already done validation for numbers as follows,
var filter =/^[0-9]+$/
But now I have to also allow hyphen and "()".
Please provide me a way for the same.
How about:
/^[0-9()-]+$/
Notes:
Parenthesis have no special meaning in a character set
If you start, or end, with a minus-sign, it is not interpreted as a range, but as the minus-character itself

Categories