Eightleven Eightleven - 1 year ago 74
Ajax Question

regex four digit validation, validating digit one by one

i have some code for validation phone number with prefix validation. i want to validate from first digit and so on up to four digits. example:

first digit

0 => valid

1-9 => invalid

second digit

8 => valid

1-7 & 9 => invalid

third digit

1 => valid

2 => valid

3-9 => invalid

four digit

7 => valid

8 => valid

9 => valid

1-6 => invalid

is it possible to validating like that with regex?

i have regex code


but not validating from first digit. please help me

Here's my php code:

<div class="container">
<div style="width:601px; height: 487px;position: fixed;">
<div style="background-image: url(xlimages/20160614_XL_elemenVisual_-02_cropped.png); background-size: 600px 563px;">
<div style="height:563px;">
<h2 style="color: white; padding-top: 32px; font-size: 50px; font-family: "Gotham Bold";"><b>Masukkan No. XL/Axis</b></h2>
<form action="<?php echo $_SERVER[PHP_SELF]; ?>" method="POST">
<div style="margin-top:70px;">
<div class="input-control">
<input type="text" name="msidn" id="jkeyboard" onlick="$('#pwd').replaceSelection("", true);" min="1" style="font-weight: bold; width:500px; height: 70px; border-color:#1ba1e2; color: #1ba1e2; font-size: 30px; text-align: center; line-height: 75px;" required>
<input type="hidden" name="cmd" value="cek"><br/><br/><br/><br/><br/><span id="spnPhoneStatus"></span>
<div style="margin-top: 190px;"><b>
<input type="submit" onclick="document.getElementById('sound1').play();" name="sbm" value="Lanjut" style="margin-bottom: 6px;background: #1ba1e2; border-color: #1ba1e2; height: 70px; font-weight: bold; margin-left:0px; width:280px; font-size: 30px;" class="button primary">

and here is my javascript code:

$(document).ready(function() {
$('#jkeyboard').blur(function(e) {
if (validatePhone('jkeyboard')) {
$('#spnPhoneStatus').html('Nomor Yang Anda Masukkan No. XL/Axis');
$('#spnPhoneStatus').css('color', 'green');
} else {
$('#spnPhoneStatus').html('Nomor Yang Anda Masukkan Bukan No. XL/Axis');
$('#spnPhoneStatus').css('color', 'red');

function validatePhone(jkeyboard) {
var a = document.getElementById(jkeyboard).value;
var filter = /^08(17|18|19|31|32|33|38|59|77|78)[0-9]{0,8}$/;
if (filter.test(a)) {
return true;
} else {
return false;


Answer Source

Thanks to combined effort of bobble bubble and me this is the final regex:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download