phone validation

Discussion in 'Scripts' started by design92, May 1, 2012.

to remove all ads.
  1. design92

    design92 New Member

    Messages:
    5
    Hi all,

    I am trying to figure out javascript for mobile / home validation.
    Just to let you know, it must be equal to 11 digits (already done it) and accept only NUMERICAL characters, so letters are not accepted once validated or when you input data.

    My issue: I don't know how to add rule to validate or do not accept letters from A-Z and other staff than numbers.

    This is my current code:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    
    <script type='text/javascript'>
    function lengthRestriction(elem, min, max){
            var uInput = elem.value;
            if(uInput.length >= min && uInput.length <= max){
                    return true;
            }else{
                    alert("Please enter " +max+ " characters");
                    elem.focus();
                    return false;
    
            }
    }
    </script>
    
    </head>
    
    <body>
    <form>
    Username(11 characters): <input type='text' id='restrict' maxlength="11"/>
    <input type='button' 
            onclick="lengthRestriction(document.getElementById('restrict'), 11, 11)"
            value='Check Field' />
    </form>
    </body>
    </html>
    
    I have also found ready code for numbers validation but I don't know how to merge these two codes, so it is validating for 11 digits, and at the same time accept only numbers.

    Code:
    <script type='text/javascript'>
    function isNumeric(elem, helperMsg){
            var numericExpression = /^[0-9]+$/;
            if(elem.value.match(numericExpression)){
                    return true;
            }else{
                    alert(helperMsg);
                    elem.focus();
                    return false;
            }
    }
    </script>
    <form>
    Numbers Only: <input type='text' id='numbers'/>
    <input type='button' 
            onclick="isNumeric(document.getElementById('numbers'), 'Numbers Only Please')"
            value='Check Field' />
    </form>
    
    Anyone have any idea how to do that? :)
     
  2. MariahWilson

    MariahWilson New Member

    Messages:
    1

Share This Page