Need Help - Telephone Number Validator

1 555-5555555-
^(1\s?) ? ((\d{3})|\d{3})[\s-]?\d{3}[\s-]?\d{4}$
for example we have number 1234.or 1456 . what we want to map is the first character should be one
so that /^1(\d{3})/ we can use this code . it wont care what are the three followed number after one.

so if i write 1234 it match or 1245 it will match . but if i say 2345 it wont match. so what i try to say is. if ^ is the before any letter or number we can only map renaming number followed by the first character.
so we got a situation we have to match 1234 and 234. what can we do now. Now hero is going to enter into movie. our hero is ‘?’ if i write ^1?(\d{3}) like this , it wont care which is the first character. if one is there (1234) it match . otherwise it can match 1234. the flag s care about space. if i have a sentence ’ i am going’ . if i want to count how many space is there i should use s flag .
so here ^(1\s?) ? ^ flag will check whether first character is one or not ; if one is there it can match or as ^(1\s?) {?} the question mark that i put inside the curly bracket (i just put that question mark inside the curly bracket to make you understand that which flag i am taking about, this flag is called look ahead. i am not sure about the name) . next flag you can see s flag followed by ^(1\s{?})? look ahead or question mark (i put that question mark inside the curly bracket). as i told you before s flag is used for counting space. for example if i want to match 12 or 1 2 ,i can match that as that s flag and s flag followed by look ahead . so now we can map only this one and space if it is there {1 space}555-555-5555 . now we have to match next three number 1 555. so did you notice that we have to match next three number and if ( bracket is there we have to match that also. for that the cod we use is
(\ (\d{3}) | \d{3}) . i will talk about this code. if you want to match a or b you can write a| b right:? the same way .(\ (\d{3}) this code will check bracket is there and \d{3}) this code will map if bracket is not there . now we matched 1 555 or 1555 or 1(555). i forgot to say. this d flag is used for matching number. /\d/ means i can match only first number if i write /\d/g d flag along with g flag i can map all number what if i want to map only specified number of digit. for example 333 i have to match this 333 . but if there is one more 3333 it should not be matched . so i can write /\d{3}/ . if i want to write 4421
the code you have to write is /\d{4} ok so we matted till 1555 . .
. Now next three character
[\s-]?\d{3}[\s-]?
i already mentioned about s flag and look ahead now only one systole left - it is not a flag. it use for mapping 1-2 whether hyphen is there or not

we matched till 1555555
now the last four character .
\d{4}$ see the code . it ends with $ sign . if any regex code end with $ that means it is the last letter or number or symbol to match.
I am sry to say I don’t know whether i can convey everything. my writing skill is a bit bad. please dont mind.

2 Likes