0

i need to validate a phone number field in my form my conditions are numbers may be 8 digits or 10 digits other than that should be give an notice the way i try is like this is this ok ??

<input class="form-control" name="phone_no" id="phone_no" placeholder="Enter a Phone Number" type="tel"  title='Phone Number(Format:04xxxxxxx)' size="10" min="8" max="10" required>

or how use the patter attribute to achieve this??pattern='\d{3}\d{4}\d{4}' like this i used but i need to check whether its between 8 and 10 less than 8 and more than 10 should give an error or notice!

Rosh_LK
  • 680
  • 1
  • 15
  • 36

5 Answers5

0

I wrote a javascript function which will detect if the number is Australian Landline, or Australian Mobile, and format them accordingly. It accepts +61 for landline and mobile, and requires Area Codes for landlines.

feel free to add to it if you think its missing functionality

    function validatePhoneNumber(phone_number){
        var formatted = "";
        //remove all non-digits
        phone_number = phone_number.replace(/\D/g,'');
        //if number starts with 61, replace 61 with 0
        if (phone_number.match(/^61/)){
              phone_number = "0"+phone_number.slice(2);
        }
        if (phone_number.match(/^04/)){
            if (phone_number.length === 10){
                var formatted = phone_number.replace(/(\d{4})(\d{3})(\d{3})/g,"$1 $2 $3");
            } else {
                alert('Invalid phone number');
            }
        } else if (phone_number.match(/^02|03|07|08/)){
            if (phone_number.length === 10) {
                var formatted = phone_number.replace(/(\d{2})(\d{4})(\d{4})/g,"($1) $2 $3");
            } else {
                alert('Invalid phone number');
            }
        } else if (phone_number.length === 8){
            alert('Please use Area Code for landline numbers');
        } else {

            alert('Invalid phone number');
        }
        //update
        $("#phone").val(formatted);
    }
NickOS
  • 764
  • 1
  • 7
  • 18
0

You can use followin javascript to validate text field length.

var textBox = document.getElementById("phone_no");

if(isNaN(textBox.value)){
  alert("Please inser a number as a phone number");
}
if(textBox.value.length < 8 || textBox.value.length > 10){
  alert("Enter text length between 8 to 10");
}
IshaS
  • 837
  • 1
  • 10
  • 31
0

First off, using the pattern attribute is not so well supported (see http://www.w3schools.com/tags/att_input_pattern.asp), so you should use javascript. A regex for a phone number has already been discussed at length here: A comprehensive regex for phone number validation.

Personally, I'd advise reading though the answer that suggests .* as a pattern.

Community
  • 1
  • 1
sudo rm -rf slash
  • 1,156
  • 2
  • 16
  • 27
-1

You can use pattern '\d{8}\d?\d?' or \d{8}|\d{9}|\d{10}.

akarilimano
  • 1,034
  • 1
  • 10
  • 17
-2

It worked for me

pattern="[0-9]{10}"
Josua Marcel C
  • 3,122
  • 6
  • 45
  • 87