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;
}
Related
I want to use regex specifically! to check if a mobile number contains 9 or more digits. I am a little unsure as to how to format this exactly.
I want to check inside an if statement like the below
if(mob >= (.{9})
This is clearly not correct, any help would be great
Use test with the regex pattern ^\+?[0-9]{9,}$:
var number = "+123456789";
if (/^\+?[0-9]{9,}$/.test(number)) {
console.log("MATCH");
}
You need to check of numbers not for all the characters. And also use test() to check if string matches regex or not.
const testMob = str => /^\+?[0-9]{9,}$/.test(str);
console.log(testMob('+123456789')) //true
console.log(testMob('+1234567890')) //true
console.log(testMob('+133333')) //false
I am trying to take only 2 characters from my phone no.
I have used regex match ^\+55 and this will return the following example.
Phone No : +5546342543
Result : 46342543
Expected Result was only 46.
I don't want to use substring for the answer instead I want to extract that from the phone no with regex.
Can anybody help me on this.
Thank you.
The pattern you used - ^\+55 - matches a literal + in the beginning of the string and two 5s right after.
46 is the substring that appears right after the initial +55. In some languages, you can use a look-behind (see example) to match some text preceded with another.
JavaScript has no look-behind support, so, you need to resort to capturing groups.
You can use string#match or RegExp#exec to obtain that captured text marked with round brackets:
var s = '+5546342543';
if ((m=/^\+55(\d{2})/.exec(s)) !== null) {
document.write(m[1]);
}
This example handles the case when you get no match.
Just try with:
'+5546342543'.match(/^\+55(\d{2})/)[1];
This will get what you want
"+5546342543".match(/^\+55(.*)/)[1]
This solves your problem ?
phoneNumber = "+5546342543"
phone = phoneNumber.substr(3) // returns "46342543"
twoDigits = phoneNumber.substr(3,2) // returns "46"
Using the substr() method as quoted :
The substr() method returns the characters in a string beginning at the specified location through the specified number of characters.
Syntax: str.substr(start[, length])
Source : Mozilla MDN
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,'));
}
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
Well, I'm pretty new on regex and in particular on JavaScript regexp.
I'm looking for making a regexp that match the hex color syntax (like #34ffa6)
Then I looked at the w3school page: Javascript RegExp Object
Then that's my regexp:
/^#[0-9a-f]{6}/i
It seems to work but, if I want it to match also the "short hex color syntax" form? (like #3fa), it's possible? I've tried using the character | but maybe I'm wrong with the syntax.
/^#[0-9a-f]{3,6}$/i
would match #abc, #abcd, #abcde, #abcdef
/^#([0-9a-f]{3}|[0-9a-f]{6})$/i
would match #abc and #abcdef but not #abcd
/^#([0-9a-f]{3}){1,2}$/i
would match #abc and #abcdef but not #abcd
/^#(?:[0-9a-f]{3}){1,2}$/i
would match #abc and #abcdef but not #abcd
Have a look at RegExp - MDN to learn more about regular expressions in javascript.
Try this :
/^#([0-9a-f]{6}|[0-9a-f]{3})$/i
[0-9a-f]{6} = 6 characters
[0-9a-f]{3} = 3 characters
$ = end
this should work
/#[0-9a-f]{6}|#[0-9a-f]{3}/gi
and for trying out regular expressions on the fly and learning it you can use this site
http://gskinner.com/RegExr/
You might want to incorporate 4 and 8 digit hex for #RGBA and #RRGGBBAA. I am doing this in a different setting where I'm using match() and split() to generate arrays. I could not get all the variations posted by #rodneyrehm to work with the g flag and match, but the first (and most verbose) solution works if I list the character count in high to low order: 8, 6, 4, 3.
let rx = /(?:#)[0-9a-f]{8}|(?:#)[0-9a-f]{6}|(?:#)[0-9a-f]{4}|(?:#)[0-9a-f]{3}/ig
let s = "123 #AA22CC 100% #12F abc #A2Cd #aa11cc44 test 236px";
let arr = s.match(rx); // arr == ["#AA22CC", "#12F", "#A2Cd", "#aa11cc44"]
The MDN docs say that (?:#) should forget "#", but it does not, and (?=#) simply fails. What am I misunderstanding?
My final goal is to include the other numeric values in the array returned from match(). I'm almost there...
The possible Hex Colors are -
Format
Example
#RGB
#F00
#RGBA
#F005
#RRGGBB
#FF7C00
#RRGGBBAA
#FF7C0016
Regexp to match color without alpha
let regex = /^#([A-F0-9]{3}|[A-F0-9]{6})$/i
Regexp to match color with alpha
let regex = /^#([A-F0-9]{3,4}|[A-F0-9]{6}|[A-F0-9]{8})$/i