Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-06-2013, 07:08 PM   PM User | #1
esthera
Senior Coder

 
Join Date: May 2004
Posts: 1,431
Thanks: 14
Thanked 0 Times in 0 Posts
esthera can only hope to improve
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?
esthera is offline   Reply With Quote
Old 03-06-2013, 07:13 PM   PM User | #2
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 946
Thanks: 7
Thanked 97 Times in 97 Posts
WolfShade is an unknown quantity at this point
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".
WolfShade is offline   Reply With Quote
Old 03-06-2013, 07:17 PM   PM User | #3
esthera
Senior Coder

 
Join Date: May 2004
Posts: 1,431
Thanks: 14
Thanked 0 Times in 0 Posts
esthera can only hope to improve
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
esthera is offline   Reply With Quote
Old 03-06-2013, 07:35 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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)
__________________

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.

Last edited by Philip M; 03-06-2013 at 07:58 PM..
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
esthera (03-06-2013)
Old 03-06-2013, 08:12 PM   PM User | #5
esthera
Senior Coder

 
Join Date: May 2004
Posts: 1,431
Thanks: 14
Thanked 0 Times in 0 Posts
esthera can only hope to improve
perfect - thanks
esthera is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:34 PM.


Advertisement
Log in to turn off these ads.