0

The below is a snippet

<form name="form1" method="post" action="index_1.php" enctype="multipart/form-data"><br />
Mobile :&nbsp;&nbsp; <input name="Mobile" id="Mobile" type="text"><br><br>
<center><input id="submit" name="submit" type="submit" value="Submit" onclick="javascript:return validateMyForm();"/></center>
</form>

and for validation, the JS is

function validateMyForm ( ) { 
    var isValid = true;
if ( document.form1.Mobile.value == "" ) { 
                alert ( "Please enter your mobile number" ); 
                isValid = false;
        }
            return isValid;
    }

How do I validate with a criteria like this " /^(+91-|+91|0)?\d{10}$/" " ???

Wilfred Clement
  • 2,674
  • 2
  • 14
  • 29
  • possible duplicate of [A comprehensive regex for phone number validation](http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation) – ops Mar 15 '14 at 07:06
  • This is country dependent. Along with post code (zip codes for the yank audience) – Ed Heal Mar 15 '14 at 07:12

2 Answers2

0

try this

var val = number.value
if (/^(+91-|+91|0)?\d{10}$/.test(val)) {
   // value is ok, use it
}
Manish Sharma
  • 2,406
  • 2
  • 16
  • 31
  • + in regex is one or more characters. ( is a start of a regex to be passed on. The regex will not compile – Ed Heal Mar 15 '14 at 07:47
0

You can use a PHP preg_match function, below is an example:

$phone = '+92-0000-0000';

if(preg_match("/^(+91-|+91|0)?\d{10}$/", $phone)) {
// $phone is valid
}

For more explanation see the link: Validate Email Address and Phone Number

*For your code it would be like this:

$phone = document.form1.Mobile.value;

if (preg_match("/^(+91-|+91|0)?\d{10}$/", $phone)) { 

        isValid = true;
}
Sohail xIN3N
  • 2,951
  • 2
  • 30
  • 29