Verify phone numbers using simple Javascript.

Xác thực số điện thoại bằng Javascript đơn giản

The content explains how to validate a phone number using JavaScript by checking it against the latest prefixes of phone networks in Vietnam. The JavaScript code uses Regex to match the phone number pattern with the allowed prefixes. If the entered phone number does not match the pattern, an error message is displayed. Examples are provided to demonstrate the validation process. Readers are encouraged to ask questions in the comments for assistance. The article aims to help users validate phone numbers accurately using simple JavaScript code.

Hey there! Are you curious about how to validate phone numbers using simple JavaScript? Well, you’re in the right place! Let’s dive in and explore the latest phone network prefixes in Vietnam:

  • Viettel: 09, 03
  • MobiFone: 09, 07
  • VinaPhone: 09, 08
  • Vietnamobile and Gmobile: 09, 05

Now, let’s take a look at the JavaScript code to validate phone numbers based on these prefixes. I’ve used a Regex method in JavaScript to check the format. Here’s a snippet of the code:

var vnf_regex = /((09|03|07|08|05)+([0-9]{8})\b)/g;
var mobile = $('#mobile').val();

if(mobile !==''){
    if (vnf_regex.test(mobile) == false) {
        alert('Số điện thoại của bạn không đúng định dạng!');
    } else {
        alert('Số điện thoại của bạn hợp lệ!');
    }
} else {
    alert('Bạn chưa điền số điện thoại!');
}

Now, let’s try some examples to see how the validation works:

  1. Enter the correct phone number with 10 digits: Verify correct phone number
  2. Enter a phone number with extra digits (11 digits): Incorrect phone number verification
  3. Enter a phone number with correct digits but the wrong prefix: Incorrect phone number verification

If you have any questions or need assistance, feel free to drop a comment below. We’re here to help!

Rate this post
See also  How to install Contact Form 7 for creating contact forms

Related posts