Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder
    Join Date
    May 2004
    Posts
    1,441
    Thanks
    15
    Thanked 0 Times in 0 Posts

    show div if number is entered

    I have a few fields that I don't allow the user to input a number

    How can i check this in javascript and if a number is entered I delete it and I put a message under it (or show a div) saying that they cannot enter numbers in that field?

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    Just curious: If you don't want user to input in field, why have the field?

    You could make it readonly, if it's a type="text" input. Or use an onfocus="this.blur();" attribute, too.
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #3
    Senior Coder
    Join Date
    May 2004
    Posts
    1,441
    Thanks
    15
    Thanked 0 Times in 0 Posts
    I want only text to be able to be entered and not numbers
    but I would like to show a message under the field if someone tries to enter in a number

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by esthera View Post
    I want only text to be able to be entered and not numbers
    but I would like to show a message under the field if someone tries to enter in a number
    Here you are:-
    Code:
    <html>
    <head>
    
    <style = "text/css">
    .message {color:red;font-size:75%; font-weight:bold; }
    </style>
    
    </head>
    
    <body>
    BOX 1 <input type = "text" id = "txtbox1" onkeyup = "noNums(this,'message1')" onblur = "noNums(this,'message1')" >
    <span id = "message1" class = "message"></span>
    <br>
    BOX 2 <input type = "text" id = "txtbox2" onkeyup = "noNums(this, 'message2')" onblur = "noNums(this,'message2')" >
    <span id = "message2" class = "message"></span>
    
    
    <script type = "text/javascript">
    
    function noNums(which, messbox) {
    
    document.getElementById(messbox).innerHTML = "";
    var val = which.value;
    var len = val.length;
    if (/\d/.test(val)) {
    val = val.substring(0,len-1);
    which.value = val;
    document.getElementById(messbox).innerHTML = "You may not enter numbers in this box!";
    }
    
    }
    
    </script>
    
    </body>
    </html>
    Only numbers are prohibited - all other characters are allowed. So someone could type "two".
    It is probably better to display the message alongside the input box as I have done rather than below it, but you can easily adjust that.



    “A man ceases to be a beginner in any given science and becomes a master in that science when he has learned that he is going to be a beginner all his life.” Robin G. Collingwood (English Philosopher, 1889-1943)
    Last edited by Philip M; 03-06-2013 at 07:58 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    esthera (03-06-2013)

  • #5
    Senior Coder
    Join Date
    May 2004
    Posts
    1,441
    Thanks
    15
    Thanked 0 Times in 0 Posts
    perfect - thanks


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •