...

View Full Version : Validate Textbox



arpan_de
05-06-2007, 01:03 AM
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):
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

rwedge
05-06-2007, 03:16 AM
You could check for numbers or text:
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

glenngv
05-06-2007, 04:01 AM
Or you can attach a trim function to the String object.


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;
}

Philip M
05-06-2007, 09:05 AM
You could check for numbers or text:
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.

rwedge
05-06-2007, 08:45 PM
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.

Philip M
05-07-2007, 08:26 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum