0
1.XXX-XXX-XXXX

2.XXXXXXXXXX

I would like to know the regular expression of the format. Modifying the existing sources will yield results.


var regExp = /^01([016789]?)-([0-9]{3})-([0-9]{4})$/;

var regExp = /^01([016789]?)[0-9]{3}[0-9]{4}$/;

A statement to check the condition. I wonder if the contact form is also correct. var test is a text field that receives input.

if(!regExp.text) {
        alert(""phone number format is not valid.");
        document.getElementById('phone').focus();
        return ;
    }
min_jeong
  • 23
  • 7
  • Possible duplicate of [JavaScript regular expression mobile phone number](http://stackoverflow.com/questions/40160958/javascript-regular-expression-mobile-phone-number) – PaulF Oct 24 '16 at 16:20
  • You have already asked this question & it has been marked as a duplicate - why are you asking again? – PaulF Oct 24 '16 at 16:21

1 Answers1

0

I'm not quite sure what you are trying to achieve, but maybe this example helps:

https://jsfiddle.net/xu9fcbxt/

Notice: jQuery required

Code:

JS:

$(document).ready(function(){
    var regExp = /^01[5-7][1-9]-[0-9]{3}-[0-9]{4}/;         

  $('#phone').focusout(function(){
    var text = $('#phone').val();   
    if(!regExp.test(text)){
        alert('not a valid phone number');
    }
  });
});

HTML:

<input id="phone" type="text" />

This would check if the number has a format like 0151-123-4567

user2718671
  • 2,866
  • 9
  • 49
  • 86