7 seconds
7 seconds

Reputation: 133

Mobile Number Javascript validation

This is my JavaScript for Mobile Number validation .

  1. Check if mobile number is valid or not.
  2. Check if mobile number contains 10 digit.

But, when I enter Valid number (without any space or dash) with 10 digit, it shows me error alert. for ex:. 4431220015

Did I miss something! Can anyone please help me or point me in the right direction!

Thanks in advance :)

JavaScript:

    var phone = document.getElementById("Telefonnummer");
    var RE = /^[\d\.\-]+$/;

    var span = document.createElement("span");
    span.innerHTML = "invalid mobile number";
    span.className = "mobilenummer";

    if (!RE.test(phone.value)) {

        swal({
            title: "error!",
            content: span,
            icon: "error",
        });
        return false;
    }

  if (phone.length != 10) {
        swal({
            title: "error!",
            content: span,
            icon: "error",
        });
        return false;
    }

HTML:

<input name="Telefonnummer" id="Telefonnummer" type="tel">  

Upvotes: 0

Views: 320

Answers (2)

jsDevia
jsDevia

Reputation: 1292

Use this kind of regular expression :

^44[0-9]{8}$

Only mobile numbers which starts with 09 and with 10 digits are allowed.

And also you could create your own regular expression with this site : regex101.com

Upvotes: 0

Tobias K.
Tobias K.

Reputation: 3082

I think your error is simply that you use phone.length, but phone is the DOM element.

You want to check phone.value.length.

Upvotes: 1

Related Questions