3

I am working on JavaScript regular expressions and as per my need text box accept only 10 digit numbers but string and special character are not allowed I tried this it but didn't work for me.

function myFunction() {
  var number = $("#num").val();
  var compare = '[0][1-9]\d{9}$|^[1-9]\d{9}$';
  if (number.match(compare)) {
    return true;

  } else {
    alert('not match');
    return false;
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="num" onblur="myFunction()">
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Anonymous
  • 1,074
  • 3
  • 13
  • 37
  • Your requirements are unclear. Why can't you simply do `^[0-9]{10}$`? – dvo Feb 20 '19 at 19:00
  • 2
    Possible duplicate of [How to allow only 4 digit numbers in html textbox?](https://stackoverflow.com/questions/34556035/how-to-allow-only-4-digit-numbers-in-html-textbox) and [How can I limit possible inputs in a HTML5 “number” element?](https://stackoverflow.com/questions/8354975) and [How to restrict user to type 10 digit numbers in input element?](https://stackoverflow.com/questions/19445408) and [validating 10 characters, can only be numbers](https://stackoverflow.com/questions/8011291) – adiga Feb 20 '19 at 19:03

4 Answers4

2

Try using this regex:

var compare=/^[0-9]{1,10}$/g
1

Use [0-9]d{10} to specify only characters from 0-9 and 10 of them.

<!DOCTYPE html>
    <html>
    <body>

    <input type="text" id="num" onblur="myFunction()">
    <script src="https://code.jquery.com/jquery-3.3.1.js"></script>

    <script>
    function myFunction() {
      var number=$("#num").val(); 
      var compare=/[0-9]{10}/;
      if(number.match(compare)){
      alert('match');
      }
      else{
      alert('not match');
      }
    }
    </script>

    </body>
    </html>

Or, better yet, do it completely with HTML:

<!DOCTYPE html>
    <html>
    <body>
     <form>
      <input type="text" id="num" pattern="[0-9]{10}" required><button>test</button>
     </form>
    </body>
    </html>
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
  • your code is not working,numbers should not more than 10 and text,special characters not allowed. – Anonymous Feb 20 '19 at 19:07
  • @Anonymous Uh, yes both examples are working. Are you asking to prevent these characters from being able to be typed in the first place or just validating after they've been typed? Because your code attempts validation after the fact so that's what I provided. – Scott Marcus Feb 20 '19 at 19:11
  • I have implemented your code in my file but still getting same error please check once – Anonymous Feb 20 '19 at 19:13
  • @Anonymous There's nothing to check, run both of my examples. They both do what you ask. What error are you getting? Most likely, it's something else on your page. – Scott Marcus Feb 20 '19 at 19:15
0

You could use this: <input type="number" size="10">

Cooper
  • 59,616
  • 6
  • 23
  • 54
0

[\d]{10} a great way to check is by using Regular Expressions 101 - https://regex101.com/ fiddle

Mr Ed
  • 83
  • 2
  • 12