Search code examples
javascriptjqueryregexvalidationphone-number

Regex french phone number


I have an input I want to limit some phone number format like

0612352512 or 0125357878

but not

0025577787 or +3327487877 or nothing else.

My regex work for some caracter, but 0000000000 or 5555555555 is always accept...

$("#tel").focusout(function(){
  var numTel = $(this).val();
  // var rgxPhone = /(([0-9]{2})[-. ]?){5}/
  var rgxPhone = /(([0-9]{2})){5}/
  if ( !numTel.match(rgxPhone)){
    $(this).removeClass().addClass("error");
    $(".nope").css({
      color:'#F70421',
      "font-weight":"bold",
      display: 'block'
    });
    $(".oke").css({display: 'none'});
  }else{
    $(this).removeClass().addClass("ok");
    $(".oke").css({
      color:'#84BD00',
      "font-weight":"bold",
      display: 'block'
    });
    $(".nope").css({display: 'none'});
  }
});
input.defaut{
  border: #9B9697 1px solid;
}

input.error{
  border: #F70421 1px solid;
}

input.ok{
  border: #84BD00 1px solid;
}

.nope, .oke{
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label for"tel">Numéro de téléphone</label>
<input type="tel" id="tel" name="tel" size="11" maxlength="10" class="defaut check">
  <p class="nope">Nope !</p>
  <p class="oke">Ok !</p>

Do you have an idea?


Solution

  • My rules ares Start by 0 Next is 1, 2, 3, 4, 5, 6, 7, 9 And the next is 8 digit suit

    Use:

    /0[1-9]\d{8}/
    

    demo