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 05-06-2007, 12:03 AM   PM User | #1
arpan_de
New Coder

 
Join Date: Sep 2002
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
arpan_de is an unknown quantity at this point
Question Validate Textbox

This is the JavaScript code which ensures that users don't keep a textbox vacant while submitting a Form (this is the onSubmit event function of the Form):
Code:
if(document.forms[0].txt1.value==""){
    alert("Textbox cannot be blank!");
    document.forms[0].txt1.focus();
    return false;
}
As such the above code works fine & prompts the user with the alert message if he tries to submit the Form without entering any text in the textbox but if the textbox is filled with spaces (by pressing the Spacebar key on the keyword), then the validation fails.

How do I overcome this?

Thanks,

Arpan
arpan_de is offline   Reply With Quote
Old 05-06-2007, 02:16 AM   PM User | #2
rwedge
Regular Coder

 
Join Date: Feb 2005
Posts: 679
Thanks: 0
Thanked 16 Times in 15 Posts
rwedge is on a distinguished road
You could check for numbers or text:
Code:
var input = document.forms[0].txt1.value;
if(input.search(/\d|\w/) == -1) {
    alert("Textbox cannot be blank!");
    document.forms[0].txt1.focus();
    return false;
}
this will allow spaces
rwedge is offline   Reply With Quote
Old 05-06-2007, 03:01 AM   PM User | #3
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
Or you can attach a trim function to the String object.

Code:
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
...
if(document.forms[0].txt1.value.trim()==""){
    alert("Textbox cannot be blank!");
    document.forms[0].txt1.focus();
    return false;
}
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 05-06-2007, 08:05 AM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,098
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by rwedge View Post
You could check for numbers or text:
Code:
var input = document.forms[0].txt1.value;
if(input.search(/\d|\w/) == -1) {
    alert("Textbox cannot be blank!");
    document.forms[0].txt1.focus();
    return false;
}
this will allow spaces
\w is equivalent to a-zA-Z0-9 and underscore. So \d is redundant. But a single valid character (even a _ ) in the textbox will return true.

Suggested improvement:-

if ((input.search(/\w/) == -1) || (input.length < 6)) {

Replace 6 by a number which is sensible in the context.

Last edited by Philip M; 05-06-2007 at 09:23 AM..
Philip M is offline   Reply With Quote
Old 05-06-2007, 07:45 PM   PM User | #5
rwedge
Regular Coder

 
Join Date: Feb 2005
Posts: 679
Thanks: 0
Thanked 16 Times in 15 Posts
rwedge is on a distinguished road
Quote:
So \d is redundant.
Thanks for pointing that out Philip. Alphanumeric+_, I latched onto words and was not thinking.

The requirements for an empty field is a little vague, what's expected.etc..
The length bit would be true if there were one character and the rest spaces.
For all we know, this field could be a students grade and A+ would be acceptable.

I think the example glenngv posted is a good approach. Reuseable, it could get rid of unwanted spaces and will catch an empty field.

Last edited by rwedge; 05-06-2007 at 08:38 PM..
rwedge is offline   Reply With Quote
Old 05-07-2007, 07:26 AM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,098
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by rwedge View Post
I think the example glenngv posted is a good approach. Reuseable, it could get rid of unwanted spaces and will catch an empty field.
Yes, but a single character, even a ? or :<) will return true. My version disregards spaces and will catch a field which is empty or of 'inadequate' length. You could of course strip any spaces as well.

I still prefer to set a minimum (or possibly maximum) length for the input depending on what is sensible or expected under the circumstances. A student's grades should accept only letters A to E as the first character, then only + and - three characters max (not min).

if (/^([A-E][\-\+]?[\-\+]?)$/i.test (grades.value)) {

An address might reasonably require 6 characters minimum. A personal name may not contain numerals (I think!). In short, the precise form of validation ought to be sensitive to the expected answer.

Last edited by Philip M; 05-07-2007 at 08:17 AM.. Reason: Improve clarity
Philip M 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 06:13 AM.


Advertisement
Log in to turn off these ads.