Editted: I am new to regular expressions. I need a regex for a 10-digit mobile number which should not start with 0 or 1 and the same number cannot be repeated for all 10 digits. Sorry for the lack of information. Please help for this problem.
Please find the html code.
<div class="col-md-2">
<label>Mobile Phone</label>
<input id="" type="text" ui-mask="(999) 999-9999" name="mobilePhone"
class="form-control"
ng-model="patientIn.addressList[0].phoneNumbers['mobile']"
ng-change="setPhoneNumber('mobile')"
ui-mask-placeholder-char="space"
model-view-value="true"/>
<p ng-show="(frmPatientEdit.$submitted && frmPatientEdit.mobilePhone.$invalid) || (frmPatientEdit.mobilePhone.$invalid && frmPatientEdit.mobilePhone.$touched)"
class="error">Mobile Phone is Invalid.</p>
Editted: I am new to regular expressions. I need a regex for a 10-digit mobile number which should not start with 0 or 1 and the same number cannot be repeated for all 10 digits. Sorry for the lack of information. Please help for this problem.
Please find the html code.
<div class="col-md-2">
<label>Mobile Phone</label>
<input id="" type="text" ui-mask="(999) 999-9999" name="mobilePhone"
class="form-control"
ng-model="patientIn.addressList[0].phoneNumbers['mobile']"
ng-change="setPhoneNumber('mobile')"
ui-mask-placeholder-char="space"
model-view-value="true"/>
<p ng-show="(frmPatientEdit.$submitted && frmPatientEdit.mobilePhone.$invalid) || (frmPatientEdit.mobilePhone.$invalid && frmPatientEdit.mobilePhone.$touched)"
class="error">Mobile Phone is Invalid.</p>
Share
Improve this question
edited Oct 20, 2016 at 14:32
OrderAndChaos
3,8702 gold badges36 silver badges64 bronze badges
asked Oct 20, 2016 at 11:29
j_ukj_uk
232 silver badges7 bronze badges
4
- My mobile number contains the sequence 6363, are you saying it's not valid? For that matter, my number contains 11 digits... – Niet the Dark Absol Commented Oct 20, 2016 at 11:30
- 2 @NiettheDarkAbsol I think OP means that you can't enter "0000000000" as valid number (so 10 times the same char). Also, different countries have different mobile number lengths ;) – Jordumus Commented Oct 20, 2016 at 11:35
- Not an asnwer as I am no regex expert but I find this site useful for figuring regexes regexr. I also don't think regex is the answer as it works sequentially and so would probably detect 11223344 as invalid too. – R Reveley Commented Oct 20, 2016 at 11:37
-
@Jordumus Oh... Yes, reading again I can see how I got mixed up there. Fair enough. I'm also guessing from the
ui-mask
that this is only accepting US phone numbers? – Niet the Dark Absol Commented Oct 20, 2016 at 11:37
5 Answers
Reset to default 1You can use this regex to check if all the numbers are the same.
(\d)
is a capturing group, \1
matches against the capturing group {9}
matches \1
9 times, you can edit that figure to suit. So This will tell you if ten numbers are the same.
(\d)\1{9}
JS
text.match(/(\d)\1{9}/g);
http://regexr./3efqd
In your case, the mobile number is of format (9999) 999-9999 Hence , the regex should be
/\((\d)\1{3}\) \1{3}-\1{4}/.test("(9999) 999-9999")
"true"
/\((\d)\1{3}\) \1{3}-\1{4}/.test("(9999) 929-9999")
"false"
here, the split-up explanation of the regex is
\(
-> matches the ( in your string
(\d)
-> matches the first integer and capture it (so that we could backreference it later)
\1
-> picks the first captured element
{3}
-> checks if the capture is repeating 3 times
(space)
-> space
\1{3}
-> checks if the capture is repeating 3 times(backreferencing)
-
-> checks for hiphen
\1{4}
-> checks if the capture is repeating 4 times
public class regex {
public static void main(String[] args)
{
String regex1 = "(\\d)\\1{9}";
String regex2 = "[\\d]{10}";
Scanner ssc = new Scanner(System.in);
System.out.println("enter string:");
String input = ssc.next();
System.out.println(input.matches(regex2)&&(!input.matches(regex1)));
}
}
If you use any formatting like a separators (-
) you can use as follows
^([2-9])(\d{2})-(\d{3})-(\d{4})(?<!(\1{3}-\1{3}-\1{4}))$
This will allow first digit as only in range of [2-9]
and phone number in format
234-234-2345
This also checks whether same number is used through out number.
Code break up:
**1st Capturing Group ([2-9])**
Match a single character present in the list below [2-9]
2-9 a single character in the range between 2 (index 50) and 9 (index 57) (case sensitive)
**2nd Capturing Group (\d{2})**
\d{2} matches a digit (equal to [0-9])
{2} Quantifier — Matches exactly 2 times
**-**
matches the character - literally (case sensitive)
**3rd Capturing Group (\d{3})**
\d{3} matches a digit (equal to [0-9])
{3} Quantifier — Matches exactly 3 times
**-**
matches the character - literally (case sensitive)
**4th Capturing Group (\d{4})**
\d{4} matches a digit (equal to [0-9])
{4} Quantifier — Matches exactly 4 times
**Negative Lookbehind (?<!(\1{3}-\1{3}-\1{4}))**
Assert that the Regex below does not match
**5th Capturing Group (\1{3}-\1{3}-\1{4})**
This group matches with character in first group at positions where
our format number will have digits.. i.e., it matches if same
number is repeated in all positions
**$**
asserts position at the end of the string
public static Boolean isValidMobileNumber(String mobileNo) {
String pattern1 = mobileNo.charAt(0) + "{10}";
String pattern2 = "[\\d]{10}";
return (mobileNo.matches(pattern1) ||!mobileNo.matches(pattern2)) ? false : true;
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745357397a4624201.html
评论列表(0条)