-1

sorry i know this question might duplicated but i'm really bad in regex, hope someone could help to fix it. How i can apply this regex in my javascript. Any help will be appreciated.

^6?01\d{8}$

My current coding.

function phoneNumber()  
{

  var mobile = document.mainform.HP_NO.value;

    //need condition here if phone number does not match with the regex given 
         alert("Not a valid Phone Number.");
         document.mainform.HP_NO.value = "";
         document.mainform.HP_NO.focus();
         return false;
    ///

     return true;
}  

<input type="text"  class="input-name" name="HP_NO" placeholder="e.g (60121234567)" onblur="if(value=='') value = '';phoneNumber();" onfocus="if(value=='') maxlength="11" onPaste="return false"/>
user3835327
  • 1,194
  • 1
  • 14
  • 44

1 Answers1

4
  1. How to make regex for phone number start with 60xxxxxxx

Your regex should be

^60\d{8}$

to check if number starts with 60

Explanation:

  • ^: Starts with
  • 60: Matches literal 60
  • \d: Matches any number/digit
  • {8}: Matches 8 times the previous group
  • $ : Ends with

Visual Representation

enter image description here


  1. How i can apply this regex in my javascript.

You can use test() to check if string satisfies regex.

var regex = /^60\d{8}$/;
if (regex.test(mobile) === false) {
    alert("Not a valid Phone Number.");
    ....
}
Tushar
  • 85,780
  • 21
  • 159
  • 179