Skip to content
Advertisement

change number of digits user inputs

I was trying to set up a signin/signup section on my website and I found a github account with codes that satisfy my target. This are the github files Anyway, the only thing I want to change is that when the user is signing up a new account, the number of digits input in the phone number has to be 8 digits instead of 10(that the github dude set). However, I don’t know how to change the part of the code into my desired result. This is the code that the github originally set in order to ensure that the user inputs exactly 10 digits.

if(!preg_match("/^[0-9]{10}+$/", $_mobile_number)) {
    $_mobileErr = '<div class="alert alert-danger">
                    Only 10-digit mobile numbers allowed.
                   </div>';
}

I think something has to be changed here as well:

if( (filter_var($_email, FILTER_VALIDATE_EMAIL)) && 
    (preg_match("/^[0-9]{10}+$/", $_mobile_number)))
)

even if I set [0-9]{10} to [0-7]{8} the problem still continues.

The issue

2

Advertisement

Answer

to check that the string is only 8 characters change {10} to {8} that is the part of the regex that tests length the [0-9] sets which numbers are allowed, of course all numbers between 0 an 9 will still be allowed, but only 8 of them

Also change the output to say the right thing too.

if(!preg_match("/^[0-9]{8}$/", $_mobile_number)) {
$_mobileErr = '<div class="alert alert-danger">
                Only 8-digit mobile numbers allowed.
               </div>';
}

Similarly in this test the {10} needs changing to {8} and the + removed it is not needed

if( (filter_var($_email, FILTER_VALIDATE_EMAIL)) && 
    (preg_match("/^[0-9]{8}$/", $_mobile_number)))
)
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement