How to validate a Phone Number With `-` and spaces [duplicate] - javascript

This question already has answers here:
How to validate phone numbers using regex
(43 answers)
Closed 9 years ago.
I want to validate the phone number Like 1-12 -123 using regex and I have tried like
/^[0-9 -]+$/
and
/^[0-9,\- ]+$/
Iam using it like
/^[0-9 -]+$/.test(value)
And it is accepting 0-9 numbers and - also but not accepting the spaces.I have tried in many ways but did'nt got any solution.Can anyone suggest me.Thanks in Advance

you can use [ -0-9]+ (leave a space after the first square bracket) to allow spaces, dashes and numbers

For javascript :
var phoneNumber = "1-12 -123";
var test = /^[0-9 -]+$/.test(phoneNumber);
alert(test); // Here, return true
If you code in PHP, this code will match your phone number.
<?php
$phoneNumber = '1-12 -123';
if(preg_match('/^[0-9 -]+$/', $phoneNumber)) {
echo "Phone number is valid";
}
Edit: Ok, use the right tag next time.

What I'd recommend is trimming out the spaces and the - from the field, and then validate the digits only.
Because I doubt
-----0-4 023 - 331 34 124
00000 -
-
etc
should be considered a valid phone-numbers?
And if you then try to make a phone-number regex to take that into considerations you'll either not succeed, or run into an expression that's so complex that it's not maintainable.
So instead trim your input/format your input into a format you control, which you then easily can validate and use that.
If you really want to go a Regular Expression route, I'd think something like
^[1-9]([ \-0-9][0-9]+)+$
is what you're after.
(if the number must not start with 0, otherwise replace the first [1-9] with [0-9])
Also change the final + into {0;y} for how many blocks of numbers you allow etc if there's a limit/focus on how many 'blocks' of numbers you allow.
Regular expressions can quickly become very complex.

this one works [0-9 -]+, this doesn't work [0-9- ]+ (order matters)

For spaces you need to put a \s in your alphabet.
/^[0-9\s-]+$/

Related

Regex failing for lower case, upper case, numbers and min 8 characters [duplicate]

This question already has answers here:
Regex for password must contain at least eight characters, at least one number and both lower and uppercase letters and special characters
(42 answers)
Closed 3 years ago.
I've looked and found so much information for regex. It's super well documented, but I'm clearly being an idiot, or have looked at this issue for too long!
The pattern I need to match is any number of upper case, lower case and numbers, with at least 8 characters. I don't want to accept anything else, such as non-alphanumeric characters (_ *^& etc)
My effort is
^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])\S{8,}$
Sadly, when I use https://regex101.com/ this does not match any of the following
aaaaaaaa
AAAAAAAA
00000000
asdfFDSA167
#fFaf9374A
12345678
123456NBh
2 of those are valid but I don't see why I'm having issues
The end goal is to use this in the pattern attribute for input (HTML 5 input pattern="" />`)
I believe this is what you're looking for: ^[a-zA-Z0-9]{8,}$
Or maybe this: ^[a-z]{8,}|[A-Z]{8,}|[0-9]{8,}$
The first one will match any combination of letters/numbers and the second one will only match sequences of like-characters. It's hard to tell exactly what you're looking for given the question.
EDIT:
I made a mistake but fixed it
Try this :
^(?=.*[A-Z])(?=.*[a-z])(?=.*[\d])([A-Za-z\d]{8,})$
DEMO

Matching the first line of content in RegEx [duplicate]

This question already has answers here:
Using regular expressions to validate a numeric range
(11 answers)
Closed 8 months ago.
I want to match numbers between 0 and 799 ONLY,if it doesn't have a comma in them.
$660
http://stackoverflow.com/questions/6560030/what-regex-can-i-use-tovalidate-anumber-between-0-and-255
I've tried using this RegEx. --> \b(0*(?:[0-9]?[0-9]?[0-9]?|100))\b and it works very well.
(If the number is between 0 and 999)
Need help with changing my regex:
I need it to work in JavaScript.
I'd like to validate the number in the first row using regex after the $ (I only need
It,if It's between 0 and 799)
If it has a comma in it then it should be ignored( like numbers 799+)
I don't want it to accept numbers with comma in them,because my current regex thinks it's valid.
(Or at least the 6,245 should be equal to 6245 so my regex can ignore it.)
To rephrase your question, numbers must not have a comma before them, or after:
(?<!,)\b[1-7]?\d?\d\b(?!,)
Try it online.
If you can't use look behinds, eg if you use JavaScript, you'll have to consume the non-comma and capture the target instead:
(^|[^,])(\b[1-7]?\d?\d\b(?!,))
The number is in group 1.
Try it online.

Regex allowing 3 numbers with no special characters and space

I am using a regex as /^-?(?:\d+|\d{1,3}(?:,\d{3})+)?$/ which allow only 3 numbers and no special characters
I need to include space check as well.
Please help me
I don't know why your regular expression is so complex. If you need three digits with no other characters, just use this:
/^[0-9]{3}$/
That will only validate strings that are three digits with nothing else, "324", "857", "111". You get it.
If you have other requirements, please update your question with them.

Regex to validate brazilian money using Javascript

I need to validate some form fileds that contain brazilian money (its name is "Real") using Javascript. It has the following format:
0,01
0,12
1,23
12,34
123,45
1.234,56
12.235,67
123.456,78
1.234.567,89
12.345.678,90
123.456.789,01
1.234.567.890,12
My regex knowledge is weak, can somebody help me please?
Does this do what you want?
^\d{1,3}(?:\.\d{3})*,\d{2}$
That says "1 to 3 digits, optionally followed by any number of groups of three digits preceded by a period, followed by a comma and two more digits." If you want to allow the leading whitespace present in your example, add \s* to the front:
^\s*\d{1,3}(?:\.\d{3})*,\d{2}$
EDIT: As #ElRonnoco pointed out, the above regular expression accepts leading zeroes (e.g. 010.100,00). To disallow those, you may use this longer version:
^\s*(?:[1-9]\d{0,2}(?:\.\d{3})*|0),\d{2}$
EDIT 2 The above regular expressions all match a string containing a single monetary amount and nothing else. It's not clear from the question if that's the intent.
EDIT 3 To allow numbers that have no decimal part, or only one decimal digit, change it like this:
^\s*(?:[1-9]\d{0,2}(?:\.\d{3})*|0)(?:,\d{1,2})?$
I would give this regex a try:
\d+(?:\.\d{3})*?,\d{2}
What it says is:
- match digits until
a. a dot followed by 3 digits is found (and this step can be repeated several times)
b. or a comma followed by 2 digits is found
EDIT:
- thanks for the comments, I forgot about the constraint for the first value
updated regex
\d{1,3}(?:\.\d{3})*?,\d{2}
Complementing Mark's reply:
Who needs "." in the string and not "," to count cents. And need find the values in middle a text :
(?:[1-9]\d{0,2}(?:\,\d{3})*|0)(?:.\d{1,2})?
https://regexr.com/61166

RegEx/Javascript validation: Don't allow comma as a valid character

I'm doing Javascript validation on my number fields. I'm using RegEx to do this - first time RegEx user. I need the user to only enter numbers and decimals, but not commas. (i.e. 3600.00 is okay, but 3,600.00 is not). I can't figure out how to eliminate the commas as an accepted character. Any help is appreciated. Thanks!
var filter = /^([0-9])/;
if (!filter.test(txtItemAmount.value))
{
msg += "Item amount must be a number.\n";
txtItemAmount.focus
}
If you want to allow decimals less than 1, integers or integers with a decimal part, you can write a reg exp for that-
/^(\.\d+)|(\d+(\.\d+)?)$/.test(value)
or you can use parseFloat-
if(parseFloat(value)+''===value)
Your filter should be something like [0-9. ]+ (here you allow numbers, . and space
A better filter would be [0-9 ]*[ .][0-9 ]* where you allow . only once.
I don't know about regex in javascript, so you may need to protect some characters with \.
try this:
^(\d+(\.\d*)?)$
It looks for one or more digits (\d+) and then, for a period followed by 0 or more digits ((\.\d*)?) . The question mark means that there has to be either 1 or 0 repetitions of the period and more digits part. THe period (.) is a special character in regex, so it has to be escaped, hence the \ before hand.
For more information, you might want to take a look here
^(\d+\.?|\d*\.\d+)$
will allow 1, .2, 3., 4.5, 12345.67890 and so on; it will disallow 1,000, 0.123,456, . or 1.2.3.
Using "lookaround," a combo of negative lookbehind and look ahead, you should be able to fail the match if a comma is present:
(?<!,)[0-9\.]*(?!,)

Categories

Resources