Regular expression for ten digit number

A B

My program here is trying to have the user input in a text field, and if the user input is a 10-digit number(numbers with 0's at the beginning or any of that sort count) when they press the check button they will receive an alert saying what their number is, or otherwise they will receive an alert telling them to enter a proper number.

However, whenever I press the button, nothing changes. I believe that there is a problem inside my regular expression and the function, but I am not sure how to write a regular expression that searches for a particular pattern where there are 10 digits in the result of a form.

Here are the relevant bits of my code.

<form name = submitnumber">
  <input type = "text" size = "15" name = "inputnumber"></input>
  <button type = "button    " onclick = "checknumber()">Check</button>
</form>
<script type "text/javascript">
  function checknumber() {
    var tendigitnum = /^\d+${10}/; 
    var input_num = document.getElementByName("inputnumber")
    if (input_num.search(tendigitnum)) {
      window.alert("You entered: " + input_num);
    } else {
      window.alert("Please enter a valid 10 digit number!");
    }
  }
</script>
gurvinder372

Your regex's syntax is wrong.

Make it

var tendigitnum = /^\d{10}$/; 

and use it as

tendigitnum.test("2342343434"); //outputs true

Also, there is no such method as getElementByName, you need to use getElementsByName and returns multiple elements. Make it

var input_num = document.getElementsByName("inputnumber")[0].value;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Regular expression - number without same digit beside

Find specific digit in a number using regular expression

Regular expression [:digit:] and [[:digit]] difference

Using a regular expression validator to check for 11 digit number or empty

Regular expression to check if number is 3 or 6 digit hex

Why is this regular expression failing to match the second 6 digit number?

Regular expression to match standard 10 digit phone number

Regular expression of two digit number where two digits are not same

regular expression on python picking only one-digit number

regular expression to search only one-digit number

regular expression for 10 digit mobile number with telephone numbers

Regular expression for number with digit length limit including decimal

regular expression match digit and characters

perl regular expression digit on string

Regular expression which accept both email address and 10 digit phone number

Regular expression to extract any 4 digit number starting beginning with 5 from a text file using python

Regular expression match between string and last digit

Regular expression: when every alternate character is a digit

Why is this regular expression only capturing the last digit?

pattern followed with no more digit regular expression python

groovy script regular expression to get digit

Email Regular Expression should NOT end with a digit/digits

regular expression: extract number in an expression

Regular expression for a hexadecimal number?

Regular expression for a mobile number

Regular Expression for number format

How to exclude the digit number in the android reg expression

Finding Ten Digit Number using regex in notepad++

Regular expression for a polish phone number