11

I need a patter like this [ 081 222 2224 ] with digits limit of 10 .. This is my try

    <form action="" method="post" id="cusCreate" autocomplete="off">
      <input type="tel" name="telphone"  pattern="[0-9]{10}"  title="Ten digits code" required/>    
      <label style="font-size:9px;padding-left:20px"> Eg : 081 222 2224  </label> 
      <input type="submit" value="Submit"/>
   </form>

http://jsfiddle.net/hbmhjt0r/

13
  • 2
    what's the problem, you want a better regex, do you have more constraints? Commented Oct 2, 2015 at 6:10
  • You can insert maxlength="10" for digits limit Commented Oct 2, 2015 at 6:17
  • i need spaces between the digits like i shown above [081 222 2224] Commented Oct 2, 2015 at 6:18
  • pattern="^\d{3}-\d{3}-\d{4}$" Commented Oct 2, 2015 at 6:21
  • 1
    don't use type "tel". Only safari supports it. Nobody uses Safari... Commented Jun 13, 2018 at 15:00

4 Answers 4

19

you can achieve your result with below changes in your code::

<form action="" method="post" id="cusCreate" autocomplete="off">
      <input type="tel" name="telphone" placeholder="888 888 8888" pattern="[0-9]{3} [0-9]{3} [0-9]{4}" maxlength="12"  title="Ten digits code" required/>    
      <label style="font-size:9px;padding-left:20px"> Eg : 081 222 2224  </label> 
      <input type="submit" value="Submit"/>    </form>

For your reference click here

JSFiddle Demo

Sign up to request clarification or add additional context in comments.

4 Comments

I think you have not checked in chrome browser, still it's working fine in firefox. good work
thank you for this solution but user can enter characters like "somthing" inside input
Thank you, even though "tel" is not a standard bye itself, the pattern works perfectly
@Gendrith it is though
6

pattern="[0-9]{3} [0-9]{3} [0-9]{4}"

This requires the user to put in spaces like this 012 345 6789. If you want the spaces to be added automatically you should add javascript to the onchange of the input. Add onchange="this.value=addSpaces(this.value);" to the input and see if it works:

function addSpaces(initial){
    initial.replace("/([0-9]{3})/","\1 ");
    initial.replace("/[0-9]{3} ([0-9]{3})/","\1 ");
    return initial;
}

Comments

4

Those from Nigeria we use phone:

new FormControl('', [Validators.required, Validators.pattern('^(080|091|090|070|081)+[0-9]{8}$')])

1 Comment

upvote because devs working with international phone numbers need to remember that patterns can be very different!
0

Not US but change accordingly:

function phoneMask() { 
    var num = $(this).val().replace(/\D/g,''); 
    $(this).val(
        0 + '(5' + num.substring(2,4) 
        +(num.length>4?')':'') 
        +(num.length>4?' '+num.substring(4,7):'') 
        +(num.length>7?' '+num.substring(7,9):'') 
        +(num.length>9?' '+num.substring(9,11):'') 
    );
}
$('[type="tel"]').keyup(phoneMask);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.