0

I have this input field that the user need to put his phone number:

<input type="text" id="Phone" />

I want to validate if the number is in the correct format. The format should be this: Only 10 digits (no letters, no "-" and no other characters) Can someone help me please?

Rabea
  • 1,938
  • 17
  • 26
  • You need to user regex which will check the pattern of your phone Num, You can use this : ^[0-9\-\+]{9,15}$ – Sami Mar 30 '16 at 17:33
  • 1
    When you did a Google search for "JavaScript validate phone number", it didn't find *anything at all*? – David Mar 30 '16 at 17:34
  • 1
    Possible duplicate of [jquery to validate phone number](http://stackoverflow.com/questions/19840301/jquery-to-validate-phone-number) – devlin carnate Mar 30 '16 at 17:34
  • @devlincarnate Current Question does not appear to be a duplicate of the linked Question ? Expected input at present Question is _"Only 10 digits (no letters, no "-" and no other characters)"_ ? – guest271314 Mar 30 '16 at 17:46
  • @HussamEddin _"The format should be this: Only 10 digits (no letters, no "-" and no other characters)"_ Does `^[0-9\-\+]{9,15}$` match `-`, `+` characters ? Up to 15 characters to be input ? – guest271314 Mar 30 '16 at 17:53
  • @guest271314 - fine. how about this one? http://stackoverflow.com/questions/2386054/javascript-phone-number-validation – devlin carnate Mar 30 '16 at 17:57
  • @devlincarnate That could be one approach. Though `javascript` is not necessary to meet requirement described at Question; see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-pattern – guest271314 Mar 30 '16 at 17:59
  • @guest271314 - my point is that this question has been answered, in several ways, before. – devlin carnate Mar 30 '16 at 18:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/107772/discussion-between-guest271314-and-devlin-carnate). – guest271314 Mar 30 '16 at 18:17

2 Answers2

0

There is a plugin for this and it is called 'Jquery mask plugin'. Also there is another question about it here. Check them out.

Community
  • 1
  • 1
Qsprec
  • 265
  • 3
  • 11
0

You can use pattern attribute with RegExp \d{10} , maxlength set to 10, title set to the message to be displayed to user at invalid submission , placeholder set to display message indicating expected format of input

<form>
  <input type="text" 
         pattern="\d{10}" 
         maxlength="10" 
         placeholder="Input 10 digits"
         title="Input 10 digits" />
  <input type="submit">
</form>
guest271314
  • 1
  • 15
  • 104
  • 177