Regex to only allow numbers under 10 digits? - javascript

I'm trying to write a regex to verify that an input is a pure, positive whole number (up to 10 digits, but I'm applying that logic elsewhere).
Right now, this is the regex that I'm working with (which I got from here):
^(([1-9]*)|(([1-9]*).([0-9]*)))$
In this function:
if (/^(([1-9]*)|(([1-9]*).([0-9]*)))$/.test($('#targetMe').val())) {
alert('we cool')
} else {
alert('we not')
}
However, I can't seem to get it to work, and I'm not sure if it's the regex or the function. I need to disallow %, . and ' as well. I only want numeric characters. Can anyone point me in the right direction?

You can do this way:
/^[0-9]{1,10}$/
Code:
var tempVal = $('#targetMe').val();
if (/^[0-9]{1,10}$/.test(+tempVal)) // OR if (/^[0-9]{1,10}$/.test(+tempVal) && tempVal.length<=10)
alert('we cool');
else
alert('we not');
Refer LIVE DEMO

var value = $('#targetMe').val(),
re = /^[1-9][0-9]{0,8}$/;
if (re.test(value)) {
// ok
}

Would you need a regular expression?
var value = +$('#targetMe').val();
if (value && value<9999999999) { /*etc.*/ }

var reg = /^[0-9]{1,10}$/;
var checking = reg.test($('#number').val());
if(checking){
return number;
}else{
return false;
}

That's the problem with blindly copying code. The regex you copied is for numbers including floating point numbers with an arbitrary number of digits - and it is buggy, because it wouldn't allow the digit 0 before the decimal point.
You want the following regex:
^[1-9][0-9]{0,9}$

Use this regular expression to match ten digits only:
#"^\d{10}$"
To find a sequence of ten consecutive digits anywhere in a string, use:
#"\d{10}"
Note that this will also find the first 10 digits of an 11 digit number. To search anywhere in the string for exactly 10 consecutive digits.
#"(?<!\d)\d{10}(?!\d)"

check this site here you can learn JS Regular Expiration. How to create this?
https://www.regextester.com/99401

Related

Regex in Javascript - Need to allow decimal as first character

I have a React widget that lets you type in a price. I put the typed-in text through a regex.test() to ensure only numbers and 1 decimal are allowed in the number. However, the regex I'm using doesn't allow a decimal point as the first character (e.g. -> .05). My current regex is
`/^(\d+\.?\d{0,9}|\.\d{1,9})$/`
I tried adding an optional \.? in the front of this, but it didn't work. Thanks for your help!
You can use
/^\d*(?:\.\d+)?$/
const testFunc = (str) =>{
return str.length > 0 && /^\d*(?:\.\d+)?$/.test(str)
}
console.log(testFunc(''))
console.log(testFunc('.123'))
console.log(testFunc('1'))
console.log(testFunc('1.123'))
console.log(testFunc('123.123.123'))

RegEx for Number and Dash/Hyphen with Certain Format

How to write JQuery/JavaScript RegEx pattern to allow only 3 numbers, 1 dash and 5 numbers OR 8 numbers?
###-##### or ######## (NO Space, NO More, NO Less and ONLY number with one dash/hyphen)
Thanks for your help.
Use .match or whatever, depending on your use case, with regex
^(\d{3}-\d{5})|\d{8}$
This conforms to your description:
\d{3}(\-)?\d{5}
And this to your example:
(\d \d{3}\-\d{5})|(\d{8})
regexPattern="[0-9]{3}[0-9-][0-9]{4}";
or
"\d{3}[\d-]\d{4}"
Tested working RegEx pattern
var pattern = /^\(?\d{3}\)?[- ]?\d{5}$/;
var val = inputs[i].value;
if(!val.match(pattern)) {
alert('Number and dash only(###-##### OR ########)');
return false;
}

Regex - getting position with Regex but without using \B [duplicate]

I am using following regex to 'insert' commas into numbers in javascript.
(\d)(?=(\d{3})+(?!\d))
It works very well with integers however when working with decimal numbers it fails cases like 10000.001223456 (result is 1,234,568.0,000,454,554)
What happens regex looks ahead after '.' finds match and replaces it with ,
Example here
I tried remedy it by adding negative lookbehind without luck,
((\d)(?=(\d{3})+(?!\d))(?<!\.))
since '.' can be at any position in sequence and I cannot use * nor +.
How do I make regex that would not match after some specific symbol (in this specific case after '.')?
You can achieve this only in 3 steps:
Split the number into integer and decimal parts
Modify the integer part
Join.
There is no variable-width look-behind in JS that would be very handy here.
var s = ".12345680000454554";
//Beforehand, perhaps, it is a good idea to check if the number has a decimal part
if (s.indexOf(".") > -1) {
var splts = s.split(".");
//alert(splts);
splts[0] = splts[0].replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,');
//alert(splts[0]);
s = splts.join(".");
alert(s);
}
else
{
alert(s.replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,'));
}

Java Script Regular expression for number format not working

I want to get the input from the user as 888-999-6666..i.e. 3 numbers then a '-' then 3 numbers then again '-' and finally 4 numbers.
I am using the following regular expression in my JavaScript.
var num1=/^[0-9]{3}+-[0-9]{3}+-[0-9]{3}$/;
if(!(form.num.value.match(num1)))
{
alert("Number cannot be left empty");
return false;
}
But its not working.
If I use var num1=/^[0-9]+-[0-9]+-[0-9]$/; then it wants at least two '-' but no restriction on the numbers.
How can i get the RE as my requirement? And why is the above code not working?
Remove the + symbol which are present just after to the repetition quantifier {} . And replace [0-9]{3} at the last with [0-9]{4}, so that it would allow exactly 4 digits after the last - symbol.
var num1=/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/;
DEMO
You could also write [0-9] as \d.
Your regex should be:
var num1=/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/;
There is an extra + after number range in your regex.
Issue in your regex.check below example.
var num='888-999-6668';
var num1=/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/;
if(!(num.match(num1)))
{
alert("Number cannot be left empty");
}else{
alert("Match");
}
In your example there is extra + symbole after {3}, which generate issue here. so i removed it and it worked.
var num1=/^\d{3}-\d{3}-\d{4}$/;
I think this will work for you.
The rest of your code will be the same.

How to check if an input box contains only numbers or commas?

Given:
var input_val = $('#field').val();
How do I check whether input_val contains only numbers or commas? The solution must work in the 4 main browers.
/^(\d|,)+$/.test(input_val) will return true as long as input_val only contains digits and/or commas.
Use the following function
function containsNumbersAndCommasOnly(str) {
return /^[0-9,]+$/.test(str);
}
by calling
if (containsNumbersAndCommasOnly(input_val)) {
}
This is a combination of the response from #Adil and a community post answer from a different post. The combination of the two works better than either individually but there are still some issues... 12,12,12 would be valid here so would 1,,,,,,,,,9 as pointed out as #mplungjan.
For my purposes this is as far as I am willing to go but someone good with regex might add a solution that detects for single instances of commas followed but not preceded by a minimum of 3 digits.
if(!(!isNaN(parseFloat(n)) && isFinite(n))){
return /^(\d|,)+$/.test(n);
}else{
return true;
}

Categories