0

I'm trying to make sure the input in a particular field is just an 11 digit number, however my if condition does not seem to be working:

Javascript:

<script>
 function check() {
 var x = document.forms["myform"]["mobile"].value;
 if (!/^\d{11}$/.test(x)) {
 myform.action="gender.html";
 else { 
 myform.action="mobilerror.html"
 }
 }
</script>

And the HTML is:

<form id="myform" onsubmit="check();" >
 <input class="input" type="text" name="mobile" required="required"
 oninvalid="this.setCustomValidity('Number is empty')" oninput="setCustomValidity('')" />
</form>

Please help!

ShawnOrr
1,4491 gold badge13 silver badges25 bronze badges
asked Jul 16, 2016 at 20:08
4
  • define "not working" Commented Jul 16, 2016 at 20:23
  • According to your code, mobilerror.html is called when the 'mobile' field does content a 11-digit number. Is that the expected behavior? Commented Jul 16, 2016 at 20:23
  • @DylanMeeus whatever I enter in the mobile field, no action is taken, neither gender.html nor mobilerror.html are loaded Commented Jul 16, 2016 at 20:38
  • @Arnauld, I can switch up which pages to call, it's just that no matter what the input is no new page is loaded and the same current page is just reloaded Commented Jul 16, 2016 at 20:39

4 Answers 4

1

You can try maxlength and type attribute of input field:

 <input class="input" type="text" name="mobile" maxlength="11" type="number" required="required"/>

If it satisfy your case then you don't need to call javascript function.

answered Jul 16, 2016 at 20:36
Sign up to request clarification or add additional context in comments.

1 Comment

And if it's a number you could change the input to type="number".
0

Your regular expression is working just fine. I think the error lies in the "if" condition. Try changing

if (!/^\d{11}$/.test(x)) {
 myform.action="gender.html";
else { 
 myform.action="mobilerror.html"
}

to this

if (/^\d{11}$/.test(x)) {
 myform.action="gender.html";
else { 
 myform.action="mobilerror.html"
}

As you can see I just took off the negation in the expression.

Note: Assuming that the "mobilerror.html" is shown when the user didn't type the 11 digit as expected.

answered Jul 16, 2016 at 20:25

1 Comment

You're a genius, and I feel stupid :D Thanks! This worked just fine!
0

Try this

 function check() {
 var x = document.forms["myform"]["mobile"].value;
 var pattern = new RegExp("^1?([1-9])(\\d{10})");
 if (pattern.test(x)) {
 myform.action="gender.html";
 } else {
 myform.action="mobilerror.html"
 }
}

^1?([1-9]) checks if the first number is not zero, so that the input value is numerically a 11-digit number. If you don't want it you can remove it.

answered Jul 16, 2016 at 20:26

Comments

0

This help you :

use type 'number':

<input type="number" id="number" class="input">

and test number digits is 11 or not with :

var patt = /.{11}/;

Example :

<html>
 <head>
 <style>
 </style>
 </head>
 <body>
 <form id="myform" onsubmit="check()" >
 <input id="number" class="input" type="number">
 <button type="submit">Submit</button>
 </form>
 <script>
 var patt = /.{11}/;
 function check(){
 var num = document.getElementById("number").value;
 var frm = document.getElementById("myform");
 if(patt.test(num))
 frm.action ="mobilerror.html"
 else
 frm.action = "gender.html";
 }
 </script>
 </body>
</html>
answered Jul 16, 2016 at 20:41

1 Comment

@Sophia111 , check my answer.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.