I need to validate phone number in PHP. I have made following regex, but I also need to it to accept numbers starting with plus char, how can I do that?
^[0-9]$
The regex you provided in your question (^[0-9]$) will only match a one digit phone number.
At the very least you probably need to add the + modifier to allow one or more digits:
^[0-9]+$
To add an optional + at the beginning, you'll need to escape it since + is a special character in regular expressions:
^\+?[0-9]+$
And finally, \d is shorthand for [0-9] so you could shorten the expression to
^\+?\d+$
use this
/^(+\d)\s((\d{3})\s*)*\d{3}(-{0,1}|\s{0,1})\d{2}(-{0,1}|\s{0,1})\d{2}$/;
Try this:
/^[\+]?([0-9]*)\s*\(?\s*([0-9]{3})?\s*\)?[\s\-\.]*([0-9]{3})[\s\-\.]*([0-9]{4})[a-zA-Z\s\,\.]*[x\#]*[a-zA-Z\.\s]*([\d]*)/
will match:
+1 ( 111)111-1111, #111
111-111-1111
(111)111-1111 x 1
111.111.1111x1
etc.
Related
I have this few strings:
'user/1/myid_print'
'user/2/myid_print'
'user/3/myid_print'
'user/4/myid_print'
'user/5/myid_print'
The second part is the dynamic one which must contain only integers. What is it's regular expression?
Try this:
/user\/\d+\/myid_print/
the \d+ matches a number which contains at least one digit.
if it is a non-zero number, replace the \d+ with [1-9]\d*
It depends a bit on what language you're using, but one valid answer for Python is:
user/\d/myid_print
The / character is often used in describing regular expressions and sometimes needs \ before it to make it match part of the string, a valid answer might be:
user\/\d\/myid_print
These match the text "user/" and "/myid_print" literally, and \d matches the pattern "any digit 0-9". If you need to match the numbers 1,2,3,4,5 only, then use [1-5] instead of \d
Test it here: https://regex101.com/r/mS4xN4/1
I have an html form with an input for a sales order number which should have the format of K1234/5678. It should always start with the letter K then 4 numbers, a / and followed by another set of 4 numbers.
I'm trying to validate the formatting using preg_match and I'm getting lost in the syntax of preg_match. From http://php.net/manual/en/function.preg-match.php I've gotten close. With the following code I'm able to verify that it contains at least 1 letter, some numbers and at least 1 non- alphanumeric value.
$so= $_POST['so'];
if (preg_match(""/^(?=.*[a-z]{1})(?=.*[0-9]{4})(?=.*[^a-z0-9]{1})/i", $so))
{
print $so;
}
What is the correct syntax to use for this? Is preg_match even the best way to do this?
Try this:
preg_match("#^K[0-9]{4}/[0-9]{4}$#i", $so)
Explanation:
The # characters are regular expression delimiters - they indicate the start/end of the pattern. The ^ and $ indicate the start and end of the string - this means that it will only match if your sales order number is the only thing in the string. The letter K means match that letter, [0-9]{4} means match a digit exactly 4 times. The i at the end means a case-insensitive match - the K will match either "K" or "k".
When developing regular expressions, I often use regular expression testers - these allow you to enter your data and try a bunch of different things to refine your regex. Google PHP regex tester to find a list of tools. Also, there's a very complete reference to regular expressions at http://www.regular-expressions.info/.
I am curious about finding a regular expression for dollars. My inputs and rules are that there can only be digits 0 to 9 and an optional deciaml point. If the decimal exists, it must have two 0 t 9 digits after it.
So it can except:
1000
1000.99
But not:
10001.1
1000.
1,000
$100.9
Do you know anything about regular expressions?
Let me explain the solution:
1)you want digits, those are [0-9]
2)you want at least one of them, which is +
3)then there may be something, lets put it into brackets, 0 or 1 times means ?
so you have now this [0-9]+(something)?
4)now you want in something to be decimal point, dot is special char in regex so you have to escape it \.
5)then you want numbers again, exactly two of them which is {2}
Here you are, full expression:
$expression="/[0-9]+(\.[0-9]{2})?/";
Here you go:
/^[0-9]+(?:\.[0-9]{2}){0,1}$/
This will check arbitrary amounts:
/^(?:[0-9]{1,3})(?:,[0-9]{3})*(?:|\.[0-9]+)$/
E.g. it will validate:
1,432.33
342
1.2
0.343
I am trying to construct a regular expression for a string which can have 0 upto 4 characters. The characters can only be 0 to 9 or a to z or A to Z.
I have the following expression, it works but I dont know how to set it so that only maximum of 4 characters are accepted. In this expression, 0 to infinity characters that match the pattern are accepted.
'([0-9a-zA-Z\s]*)'
You can use {0,4} instead of the * which will allow zero to four instances of the preceding token:
'([0-9a-zA-Z\s]{0,4})'
(* is actually the same as {0,}, i.e. at least zero and unbounded.)
If you want to match a string that consists entirely of zero to four of those characters, you need to anchor the regex at both ends:
'(^[0-9a-zA-Z]{0,4}$)'
I took the liberty of removing the \s because it doesn't fit your problem description. Also, I don't know if you're aware of this, but those parentheses do not form a group, capturing or otherwise. They're not even part of the regex; PHP is using them as regex delimiters. Your regex is equivalent to:
'/^[0-9a-zA-Z]{0,4}$/'
If you really want to capture the whole match in group #1, you should add parentheses inside the delimiters:
'/(^[0-9a-zA-Z]{0,4}$)/'
... but I don't see why you would want to; the whole match is always captured in group #0 automatically.
You can use { } to specify finite quantifiers:
[0-9a-zA-Z\s]{0,4}
http://www.regular-expressions.info/reference.html
You can avoid regular expressions completely.
if (strlen($str) <= 4 && ctype_alnum($str)) {
// contains 0-4 characters, that are either letters or digits
}
ctype_alnum()
I use this preg_match condition for matching positive, negative and decimal values
/^[0-9,-\.]{1,50}$/
But when I enter --34.000 it does not show error, when I enter 34...9868 it does not show error,
what I want is that it must accept only positive, negative and decimal values.
Better if you use something like is_numeric() if yuo need to check if it's a number.
And your regex is totally broke because as now it can accept even only a string containing 50 dots
As yes123 stated, there are better ways to detect if a given input string is a numeric value. If you'd like to stick to regular expressions, the following might be OK for you:
/^-?[0-9]+(?:\.[0-9]+)?$/
Explanation:
match start of the string ( ^)
match a possible - character (-?); the ? means "not required"
match at least one number ([0-9]+)
possibly match the whole statement in the parentheses ((?:...)?); ?: means "do not capture the subpattern"
a point (\.); the . needs to be escaped due to its special function
at least one number ([0-9]+)
match end of the string ($)
You need to split up your regular expression so that it only accepts the characters in the right places. For example:
/^[+\-]?([0-9]+,)*[0-9]+(\.[0-9]+)?$/
To explain this expression:
[+\-]?: This checks for a + or - prefix to the number. It's completely optional, but can only be a + or -.
([0-9]+,)*: This allows an optional set of comma-delimited numbers. This is for the thousands, millions etc.
[0-9]+: This requires that the value contains at least some numbers
(\.[0-9]+)?: Finally, this allows an optional decimal point with trailing numbers.
try this regex ^-?\d*\.?\d+$
i suppose it however cannot be limited to 50 chars