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 01-22-2013, 06:04 AM   PM User | #1
tpeck
Regular Coder

 
tpeck's Avatar
 
Join Date: Oct 2002
Location: Sydney, Australia
Posts: 771
Thanks: 40
Thanked 5 Times in 4 Posts
tpeck is on a distinguished road
validate input

I need to check the input of a textarea. I require the cursor not to allow input of anything except "n" or "N" or "y" or "Y".

I have this so far:

Code:
function validate() {
    var val = document.getElementById('textarea').value;
    if (/^\s*$/g.test(val)) {
        alert('Wrong content!');
    }
}
It checks for empty spaces I think.

How can it check for yes or no? (y or Y or n or N)
__________________
The difference between genius and stupidity is that genius has its limits. (Albert Einstein)
tpeck is offline   Reply With Quote
Old 01-22-2013, 07:29 AM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Code:
<input type = "text" id = "textarea" size =  "1" maxlength = "1" onblur = "validate()">

<script type = "text/javascript">
function validate() {
var val = document.getElementById('textarea').value;
if (/[^yn]/gi.test(val)) {
alert ('Wrong content!  You must enter y or n');
document.getElementById('textarea').value = "";
return false;
}
}
</script>

It is a bad idea to name an HTML field "textarea". And why a textarea if the only valid entry is y or n?


Due to earlier signalling problems there are short-notice castrations in the Guildford area. - Train announcer at Wimbledon Station.
__________________

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; 01-22-2013 at 07:32 AM..
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
tpeck (01-22-2013)
Old 01-22-2013, 07:44 AM   PM User | #3
tpeck
Regular Coder

 
tpeck's Avatar
 
Join Date: Oct 2002
Location: Sydney, Australia
Posts: 771
Thanks: 40
Thanked 5 Times in 4 Posts
tpeck is on a distinguished road
Thanks Philip.

I have changed it to an <input> with the id="done":

Code:
<input type="text" class="Format4" id="done" name="done" size="1" maxlength="1" value="N">
It works!
__________________
The difference between genius and stupidity is that genius has its limits. (Albert Einstein)
tpeck 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 08:59 PM.


Advertisement
Log in to turn off these ads.