what's wrong with this regular expression (if else regex) - javascript

What I want is, there is a textbox with maximum length of 5. The values allowed are..
any integer // for example 1, 3, 9, 9239 all are valid
real number, with exaclty one point after decimal // eg. 1.2, 93.7 valid and 61.37, 55.67 invalid
it is also allowed to enter only decimal and a digit after that, that is .7 is valid entry (would be considered as 0.7)
I found this page, http://www.regular-expressions.info/refadv.html
So what I thought is that
There is a digit
If there is a digit and a decimal after that, there must be one number after that
If there is no digit there must be a decimal and a digit after that
So, the regex I made is..
a single digit one or more => /d+
an optional decimal point followed by exactly one digit => (?:[.]\d{1})?
if first condition matches => (?(first condition) => (?((?<=\d+)
then, match the option decimal and one exact digit =>(?((?<=\d+)(?:[.]\d{1})?
else => |
find if there is a decimal and one exact digit => (?:[.]\d{1}){1}
check the whole condition globally => /gm
overall expression =>
(?(?<=\d+)(?:[.]\d{1}){1}|(?:[.]\d{1}){1})+/gm
But it doesn't outputs anything..
Here's the fiddle
http://jsfiddle.net/Fs6aq/4/
ps: the pattern1 and pattern2 there, are related to my previous question.

Maybe you are complicating things too much. I did a quick test and unless I'm missing something this regex seems to work fine:
/^\d*\.?\d$/
Demo: http://jsbin.com/esihex/4/edit
Edit: To check the length you can do it without regex:
if ( value.replace('.','').length <= 5 && regex.test( value ) ) {
...
}
Notice that I used replace to remove the dots so they don't count as characters when getting the length.

You can try the following pattern:
/^\d{0,4}\.?\d$/
It seems to fulfil all your requirements:
> /^\d{0,4}\.?\d$/.test(".4")
true
> /^\d{0,4}\.?\d$/.test(".45")
false
> /^\d{0,4}\.?\d$/.test("1234.4")
true
> /^\d{0,4}\.?\d$/.test("12345.4")
false
> /^\d{0,4}\.?\d$/.test("12345")
true
> /^\d{0,4}\.?\d$/.test("123456")
false
This pattern assumes that the number can have a maximum of five digits and an optional decimal point.
If the maximum length of five includes the optional decimal point then the pattern is slightly more complex:
/^(?:\d{1,5}|\d{0,3}\.\d)$/
The first part of the group deals with integer numbers of the required length, the second option of the group deals with real numbers which maximum length (including the decimal point) is five.

Consider this code:
var checkedString = "45.3 fsd fsd fsdfsd 673.24 fsd63.2ds 32.2 ds 32 ds 44 fasd 432 235f d653 dsdfs";
checkedString = " "+checkedString;
var results = checkedString.match(/[\s]{1}(\d+\.*\d{1})(?![\d\.\w])+/gm);
results.map(function(result) {
return result.trim();
});
Couldn't make it in other way because in JS (?<= (lookbehind) regexp is not working.
This will be returned:
["45.3","32.2","32","44","432"]
So probably it's what you've expected.

I don't know what are you trying to do with those conditionals in your regex. I also looked at your jsfiddle, which outputs nothing for me. But I made a two versions of a regex that matches the correct values for the textbox, which are ^(?!(.{6,}))(?:[1-9]\d*)*(?:\.\d*[1-9])?$ and ^(?!(.{6,}))(?:\d*)*(?:\.\d*)?$.
The first disallows to start with zero, or end with zero after the decimal.
Comment if you need explanation of the regex.

Related

Regex for single digits and single digit with decimal only

I'm having a hard time coming up with the most suitable regex for my needs.
I want a regex that only validates numbers from 1-8, with or without a decimal point
1 // Valid
3 // Valid
8 // Valid
2.00 // Valid
4.45 // Valid
7.60 // Valid
9 // Invalid
10 // Invalid
9.00 // Invalid
50.40 // invalid
So far this is what I came up with, but ^([0-8]$\.?[0-8]*|\.[0-8]+) but this only accepts numbers from 1-8, and nothing with a decimal. Can someone kindly provide a suitable regex for this example?
I guess it would be better to limit regex to string parsing. My best approach would be to parse a valid number from the string and then convert it to number before doing any validation on its value as being < x
But if you really want to go via regex (and it will be hard to change in case) would be with something like:
^0*([0-7](\.\d+)?|8)$
It matches any number between 0 and 8 ... decimal point is allowed if followed by numbers and the highest valid number like that would be 7.999999999... followed by the next valid integer number being 8.
Trailing zeros are considered.

Regular expression to ignore the numbers after length reaches 10

I want number in this format
(123)-456-7890
The maximum length assigned is 10.
The regular expression used to obtain the above format is:
if (onlyNums.length === 10) {
const number = onlyNums.replace(/(\d{3})(\d{3})(\d{4})/, '($1) -$2-$3');
If length>10 I want the above format for the number and to ignore the rest of the digits(right trim).
How can I do that?
If you remove the if condition and add a "catch-all" regex .* at the end, it will ignore whatever comes after the 10th digit:
const number = onlyNums.replace(/(\d{3})(\d{3})(\d{4}).*/, '($1) -$2-$3');
This assumes that onlyNums actually contains nothing but digits (and at least 10 of them). Otherwise, the result might be unexpected.
Test it live on regex101.com.

Regular Expression in JS for numbers

I'm looking for a regular expression in javascript that takes care of this:
Accept only numbers between 6 and 15 digits, 6 is the minimum.
Numbers cannot contain groups of repeated digits, such as 408408 or 123123
Numbers cannot contain only two different digits, such as 121212
I started with this, then I was loss
^[0-9]{6,15}$
Instead of just a Regex, use a combination of if-statements and a RegEx.
function validateNumber() {
var numbers = document.getElementById('numbers1').value;
if (numbers && !isNaN(numbers)) {
// make sure a that something was entered and that it is a number
if (numbers.length < 6 || numbers.length > 15) {
// make sure number is between 6 and 15 digits
alert('Number must be between 6 and 15 digits long.');
} else if (numbers.match(/(.+)\1+/)) {
// make sure that the numbers contain no repeated digits
alert('Number cannot be repeated.');
} else {
alert('Number validated!');
// otherwise, the number is validated
}
} else {
// if no number was entered
alert('Please enter a number.');
}
}
<input type="text" placeholder="enter numbers" id="numbers1" />
<input type="button" value="Validate!" onclick="validateNumber()" />
You have the first rule correct:
^\d{6,15}$
That covers both the 6-15 length requirement and the fact that it has to be numeric.
With the next rule, it's easier to test for repeated substrings than to test for their absence:
(.+)\1
The last one is a lot more complicated. Here's how you test for at least 3 distinct characters:
(.+)(?!\1)(.+)(?!\1|\2).
Put it all together and what do you get:
^(?=\d{6,15}$)(?!.*(.+)\1)(.+)(?!\2)(.+)(?!\2|\3).+$
That answers your question as written, but as I said in the comments, you should consider very carefully whether you're starting from the right assumptions. You don't have to use regex for this, nor do you have to do it all in one regex. Will the pattern above be easy for you to work with when you come back in 6 months and have to change the rules?
More importantly, if you're trying to make sure users pick a strong password, the rules you're using will weaken your security by reducing the number of possible choices. And the maximum length of 15 characters suggests you're storing passwords in plain text. You should be hashing them.
Here's my attempt, just for the challenge:
^(?!\d*?(\d+?)\1)\d{6,15}$
Demo
The (?!\d*?(\d+?)\1) part will make sure there are no groups of repeated digits by matching a group of digits and trying to match the same digits immediately after. If it finds one, the match fails.
If you want to allow two same consecutive digits, replace (?!\d*?(\d+?)\1) with (?!\d*?(\d{2,}?)\1)(?!(\d)\2*(\d)(?:\2|\3)*$). This will then make sure there are more than 2 different digits by matching a series of one digit, then a different digit followed by a series of a combination of both digits. If it reaches the end of the string it forces the match to fail.
But it'll be probably more maintainable to just do it the conventional way, without regex.

Regex for number with decimals and thousand separator

I need regex to validate a number that could contain thousand separators or decimals using javascript.
Max value being 9,999,999.99
Min value 0.01
Other valid values:
11,111
11.1
1,111.11
INVALID values:
1111
1111,11
,111
111,
I've searched all over with no joy.
/^\d{1,3}(,\d{3})*(\.\d+)?$/
About the minimum and maximum values... Well, I wouldn't do it with a regex, but you can add lookaheads at the beginning:
/^(?!0+\.00)(?=.{1,9}(\.|$))\d{1,3}(,\d{3})*(\.\d+)?$/
Note: this allows 0,999.00, so you may want to change it to:
/^(?!0+\.00)(?=.{1,9}(\.|$))(?!0(?!\.))\d{1,3}(,\d{3})*(\.\d+)?$/
which would not allow a leading 0.
Edit:
Tests: http://jsfiddle.net/pKsYq/2/
((\d){1,3})+([,][\d]{3})*([.](\d)*)?
It worked on a few, but I'm still learning regex as well.
The logic should be 1-3 digits 0-1 times, 1 comma followed by 3 digits any number of times, and a single . followed by any number of digits 0-1 times
First, I want to point out that if you own the form the data is coming from, the best way to restrict the input is to use the proper form elements (aka, number field)
<input type="number" name="size" min="0.01" max="9,999,999.99" step="0.01">
Whether "," can be entered will be based on the browser, but the browser will always give you the value as an actual number. (Remember that all form data must be validated/sanitized server side as well. Never trust the client)
Second, I'd like to expand on the other answers to a more robust (platform independent)/modifiable regex.
You should surround the regex with ^ and $ to make sure you are matching against the whole number, not just a subset of it. ex ^<my_regex>$
The right side of the decimal is optional, so we can put it in an optional group (<regex>)?
Matching a literal period and than any chain of numbers is simply \.\d+
If you want to insist the last number after the decimal isn't a 0, you can use [1-9] for "a non-zero number" so \.\d+[1-9]
For the left side of the decimal, the leading number will be non-zero, or the number is zero. So ([1-9]<rest-of-number-regex>|0)
The first group of numbers will be 1-3 digits so [1-9]\d{0,2}
After that, we have to add digits in 3s so (,\d{3})*
Remember ? means optional, so to make the , optional is just (,?\d{3})*
Putting it all together
^([1-9]\d{0,2}(,?\d{3})*|0)(\.\d+[1-9])?$
Tezra's formula fails for '1.' or '1.0'. For my purposes, I allow leading and trailing zeros, as well as a leading + or - sign, like so:
^[-+]?((\d{1,3}(,\d{3})*)|(\d*))(\.|\.\d*)?$
In a recent project we needed to alter this version in order to meet international requirements.
This is what we used: ^-?(\d{1,3}(?<tt>\.|\,| ))((\d{3}\k<tt>)*(\d{3}(?!\k<tt>)[\.|\,]))?\d*$
Creating a named group (?<tt>\.|\,| ) allowed us to use the negative look ahead (?!\k<tt>)[\.|\,]) later to ensure the thousands separator and the decimal point are in fact different.
I have used below regrex for following retrictions -
^(?!0|\.00)[0-9]+(,\d{3})*(.[0-9]{0,2})$
Not allow 0 and .00.
','(thousand seperator) after 3 digits.
'.' (decimal upto 2 decimal places).

Currency validation

Please help with me writing a JavaScript Validation for currency/money field.
So please provide any regular expressions if u have :)
Also, for my region, don't need any currency symbols like '$' in the field.
Only decimals are to be included for validation as special chars., along with numbers.
You could use a regexp:
var regex = /^\d+(?:\.\d{0,2})$/;
var numStr = "123.20";
if (regex.test(numStr))
alert("Number is valid");
If you're not looking to be as strict with the decimal places you might find it easier to use the unary (+) operator to cast to a number to check it's validity:
var numStr = "123.20";
var numNum = +numStr; // gives 123.20
If the number string is invalid, it will return NaN (Not a Number), something you can test for easily:
var numStr = "ab123c";
var numNum = +numStr;
if (isNaN(numNum))
alert("numNum is not a number");
It will, of course, allow a user to add more decimal places but you can chop any extra off using number.toFixed(2) to round to 2 decimal places. parseFloat is much less strict with input and will pluck the first number it can find out of a string, as long as that string starts with a number, eg. parseFloat("123abc") would yield 123.
I built my answer from the accepted answer.
var regex = /^[1-9]\d*(((,\d{3}){1})?(\.\d{0,2})?)$/;
^[1-9] The number must start with 1-9
\d* The number can then have any number of any digits
(...)$ look at the next group from the end (...)$
(...)?(...)? Look for two groups optionally. The first is for the comma, the second is for the decimal.
(,\d{3}){1} Look for one occurance of a comma followed by exactly three digits
\.\d{0,2} Look for a decimal followed by zero, one, or two digits.
This regex works off of these rules:
Valid values are numbers 0-9, comma and decimal point.
If a customer enters more than one decimal point or more than one comma, the value is invalid and will not be accepted.
Examples of invalid input values
1.2.3
1,2,4
Examples of valid input values
1.23
1,000
3967.
23
1.2
999,999.99
An example can be seen here:
http://jsfiddle.net/rat141312/Jpxu6/1/
UPDATE
by changing the [1-9] in the regex to [0-9] any number less than 1 can also be validated. Example: 0.42, 007
/[1-9]\d*(?:\.\d{0,2})?/
[1-9] - must start with 1 to 9
\d* - any number of other digits
(?: )? - non capturing optional group
\. - a decimal point
\d{0,2} - 0 to 2 digits
does that work for you?
or maybe parseFloat:
var float = parseFloat( input );
let amount = document.querySelector('#amount'), preAmount = amount.value;
amount.addEventListener('input', function(){
if(isNaN(Number(amount.value))){
amount.value = preAmount;
return;
}
let numberAfterDecimal = amount.value.split(".")[1];
if(numberAfterDecimal && numberAfterDecimal.length > 3){
amount.value = Number(amount.value).toFixed(3);;
}
preAmount = amount.value;
})
<input type="text" id="amount">
For me its working fine for Indian currency in INR
var regex = /^[1-9]{0,2}(,{0,1})(\d{2},)*(\d{3})*(?:\.\d{0,2})$/;
var a = '1,111.11';
regex.test(a);
Now I use this:
let func = function (vStr) {
let v0 = Number(vStr);
let v1 = Number(v0.toFixed(2));
return v0 === v1;
};
Note that, NaN === NaN returns false. Maybe some substitution for '$' and ',' before parsing is needed, for other cases.
And there is a problem of precision for very large number, longer than 16 digits. As well as values of '0x3a', '68n' is considered valid.
Nowadays, <input> of type="number", with step='.01' may be more proper.

Categories