Erwin van Ekeren
Erwin van Ekeren

Reputation: 730

Check if field contains at least a number of digits

Currently i am using the following script to check if my field is empty or not. For me this script works great, the only thing I want to add to the script is a check that the field needs at least 10 digits (numbers) in it too otherwise just shows the error message as in the script. I hope someone can help me find out what needs to be added to the code below

function checkforblank() {

var errormessage = "";

    if (document.getElementById('telefoonnummer').value == "") {
    errormessage += "your 10 digits phone number is missing " ;
    document.getElementById('telefoonnummer').style.bordercolor = "red";
    }


if (errormessage != "") {
    $(".error-messages").text(errormessage).fadeIn();
    return false;
}

    }

Upvotes: 1

Views: 2554

Answers (3)

Harry
Harry

Reputation: 89750

If all you need is a plain length check on submit and the field can have only digits, you can use the below to check for length.

function checkforblank() {

    var errormessage = "";
    var inputVal = document.getElementById('telefoonnummer').value;
    if (inputVal == "") {
        errormessage += "your 10 digits phone number is missing " ;
        document.getElementById('telefoonnummer').style.bordercolor = "red";
    } 
    else if (inputVal != "" && inputVal.length !== 10) {
        console.log("error");
        //set your error message here
    } 
    else {
        console.log("success");
    }

    if (errormessage != "") {
        $(".error-messages").text(errormessage).fadeIn();
        return false;
    }

}

Update: Revised Version based on OP feedback - fiddle

Upvotes: 1

K Cloud
K Cloud

Reputation: 271

    //this is to check weather it is a number or not------
    function checkNumber(event,val){//use this function at keyup event of your field
    if(val.length <= 10) //check wether it is 10 digit or not
    {   
    if ( event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || event.keyCode == 13 || 
                     // Allow: Ctrl+A
                    (event.keyCode == 65 && event.ctrlKey === true) || 
                     // Allow: home, end, left, right
                    (event.keyCode >= 35 && event.keyCode <= 39)) {
                         // let it happen, don't do anything
                         return;
                }
                else {
                    // Ensure that it is a number and stop the keypress
                    if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105 )) {
                        event.preventDefault(); 
         }

                       }   
                    }
            }

use the code with the your text field like this

<input type="text" id="phone" onkeyup = "checkNumber(event,this.value);" />

Upvotes: 0

Paul S.
Paul S.

Reputation: 66324

Remove all non-digits and take the length

'a0b123c456v789---'
    .replace(/[^\d]/g, '') // "0123456789"
    .length;               // 10

Upvotes: 3

Related Questions