Search code examples
inputminimum

Set Minimum input value in Ajax/java script


This code shows error message when there is no value entered. How do i create a coding to have an minimum input of 8 characters?

For Null value:

if ($("shipping_tel")) {
            if ($("shipping_tel").value === "") { 
                ErrorMsg += " - Shipping Address: Phone Number\n";
                $("shipping_tel_label").className = 'hilight';
            } else {
                $("shipping_tel_label").className = 'lolight';      
            }
        }

For minimum input value:

if ($("shipping_tel")) {
            if ($("shipping_tel").value < 8) { 
                ErrorMsg += " - Shipping Address: Phone Number\n";
                $("shipping_tel_label").className = 'hilight';
            } else {
                $("shipping_tel_label").className = 'lolight';      
            }
        }

but this does not work. Any idea?


Solution

  • Try this

    str = $("shipping_tel").value;
    //This regular expression checks the string.
    var patt=/[0-9]{8,}/g;  //atleast 8 nums long.
    if(patt.exec(str)!=str){
        ErrorMsg = " - Shipping Address: Phone Number\n";
        $("shipping_tel_label").className = 'hilight';
    }else{
        $("shipping_tel_label").className = 'lolight';
    }