This question already has an answer here:
Regular expression to validate US phone numbers? [duplicate]
(1 answer)
Closed 5 years ago.
I am trying to write a regex for US phone numbers where user can enter the number and dash comes automatically.
But here the drawback is if user enters "123" and then "-" the regex breaks and instead of 123-456-7890 it becomes 123-4567890
Here is the regex code:
$('#AccountFrm_telephone').attr('maxlength', '12');
$('#AccountFrm_telephone').keyup(function(){
$(this).val($(this).val().replace(/^(\d{3})(\d{3})(\d)+$/, "$1-$2-$3"));
});
Maybe There is something that we add in regex so that user can not type dash?
for completes:
you have 2 issues
first this part (\d)+ should be (\d+) or you wont capture the last group on numbers correctly.
the second part is that you aren't handling possible dashes in the input, so try something like:
.replace(/^(\d{3})-?(\d{3})-?(\d+)$/, "$1-$2-$3")
the question marks (?) denote 0 or 1 times, meaning the user can input the dashes if he wants
Maybe try the following:
$('#AccountFrm_telephone').attr('maxlength', '12');
$('#AccountFrm_telephone').keyup(function(){
$(this).val($(this).val().replace(/(\d{3})(\d{3})(\d{4})/, "$1-$2-$3"));
});
Try this:
$("#input1").on('keyup', e => {
e.target.value = e.target.value.replace(/(\d{3})-?(\d{3})-?(\d+)/, '$1-$2-$3')
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="input1">
Related
This question already has an answer here:
javascript regexp replace not working, but string replace works
(1 answer)
Closed 1 year ago.
Hello team I am new to JS so I am trying to use RegEx with replacing to take input from the user and replace it if it doesn't match the RegEx I have to be able to put 7 digits or 6 digits followed with one letter currently I am doing this
someID.replace('^(([0-9]{1,7})|([0-9]{1,6}[a-zA-Z]{1}))$')
I am not able to replace the current string with the RegEx expression if I enter
12345678900 it remain the same in that situation I need to be 1234567 after the replace or if I have 12345678asd to be 123456a. How can I achieve that by only replace function and a RegEx expresion
You need to use a different regex and a dirrent replace function.
You will also need to get rid of $ if you want to be able to successfully match the string, without worrying about how it ends.
const sampleIDs = [
"123456789000",
"123456abc",
];
sampleIDs.forEach(id => {
const clean = id.match(/^\d{6}[\d\D]/);
console.log(clean[0]);
});
This question already has an answer here:
Escape string for use in Javascript regex [duplicate]
(1 answer)
Closed 3 years ago.
I have the following code to filter a list based on what the user types in an input field:
const searchQuery = e.target.value.trim();
items
.filter((item) =>
item.title.match(new RegExp(searchQuery, 'gi'))
)
.map(item => {
// Do something...
});
it works great except everything breaks when the user enters some funky text in the input field like:
//on\/: \/: \
Results in the following error:
SyntaxError: Invalid regular expression: ///on\/: \/: \/: \ at end of pattern
How to approach this correctly so that my code doesn't break based on user input?
Sounds like you need to perform escapes, or some type of validation. Here's a link to the JavaScript escape() Function. I hope that helps.
This question already has answers here:
How to detect exact length in regex
(8 answers)
Closed 4 years ago.
So I am trying to use a regular expression to check against strings but it doesn't seem to be working properly.
Basically I want it to match a alpha-numeric string that is exactly 3 characters long. The expression I am using below does not seem to be working for this:
const msg = message.content;
const regex = /[A-Za-z0-9]{3}/g;
if (msg.match(regex)) {
// Do something
}
Am I doing something wrong? Any help would be appreciated. Thanks in advance.
You need to add ^ and $ for the start-of-string anchor and end-of-string anchor, respectively - otherwise, for example, for #123, the 123 will match, and it will pass the regex. You also might consider using the i flag rather than repeat A-Za-z, and you can use \d instead of 0-9.
It looks like you just want to check whether the string passes the regex's test or not, in which case .test (evaluates to a boolean) might be a bit more appropriate than .match. Also, either way, there's no need for the global flag if you're just checking whether a string passes a regex:
const regex = /^[a-z\d]{3}$/i;
if (regex.test(msg)) {
// do something
}
This question already has answers here:
Numeric validation with RegExp to prevent invalid user input
(3 answers)
Closed 7 years ago.
I need a regex for any number OR float.
I have used this but doesn't work:
/(^[0-9]+*[.][0-9]+)$|^[\d+]$/
Why ?
Try this regex:
/^[+-]*[0-9]+[.][0-9]+|[+-]*[0-9]+$/g
You can use http://www.regexr.com/ to test and create regexes
Try this one:
^[-+]?[0-9]*\.?[0-9]+$
In case this might help you. This online Regex Tools is actually very helpful:
http://www.regexr.com/
You have a few issues with your regex:
You cannot have a quantifier after + so change +* to just + (1 or more matches)
Move your start identifier ^ outside of the group, for consistency
hmmm, you seem to have edited your regex: but [^\d+] was looking for NOT a number or + symbol.
One possible solution would be as follows:
^([0-9]+[.][0-9]+)$|^\d+$
Here is a working example
For more example, see here
You expression doesn't work because it contains errors.
This is the website I use for RegEx testing - It is good to test them on a website like this that gives you feedback
You have the beginning inside a capturing group, but the end outside - (^...)$
You also have double operators - +* - Use only one
And the or not plus sign and digit is not needed - ?[^\d+]
I believe this expression will do what you want: ^-?\d+(\.\d+)?$
This question already has answers here:
Regex phone number to accept + [duplicate]
(4 answers)
Closed 3 years ago.
I want to validate a phone number this format +905555555555.
How can I write a regex expression to test for this?
one "+" and 12 numbers. hint: escape the "+".
^\+(90)\([2-5]{1}\)[0-9]{9}
or not starts with +;
\+(90)\([2-5]{1}\)[0-9]{9}
<script type="text/javascript">
var test = "+905555555555";
var re = new RegExp(/^\+[0-9]{12}$/gi);
if(re.test(test))
{
alert("Number is valid");
} else {
alert("Number is not valid");
}
</script>
Check out this site for testing out your Regular Expressions: http://www.regextester.com/index2.html
And a good starting point to learn is here:
http://www.regular-expressions.info/
if you need it to start with a "+" and a "9" and 11 digits:
^[+]9\d{11}$
I recommend that you understand how regEx work, take a look at this usefull tester:
http://www.sweeting.org/mark/html/revalid.php
At the bottom they explain what each operator means.
Also there are all sort of examples at the internet.
EDIT: after reading the OP's comments, in order for the number to start with "+90" and then have 10 digits, you can use the following expression:
^[+]90\d{10}$
To cover your additional specifications use this:
^\+90\([2-5]\)\d{9}$
You definitely need the anchors ^ and $ to ensure that there is nothing ahead or after your string. e.g. if you don't use the $ the number can be longer as your specified 12 numbers.
You can see it online here on Regexr
If it's exactly that format then you could use something like: /^\+\d{12}\z/
If you'd like to allow some spaces/dashes/periods in it but still keep it at 12 numbers:
/^\+(?:\d[ .-]?){12}\z/
Then you could remove those other chars with:
s/[ .-]+//g (Perl/etc notation)
Adjusted a bit to answer question for a 12 digit regex.
// +905555555555 is +90 (555) 555-5555
let num = '+905555555555';
/^\+90\d{10}$/.test(num);