Hi, I'm trying to validate some mandatory fields to be non-spaces input. I tried isEmpty() but it regard the spaces as 'empty'. Thus spaces are still submitted; I want client-side validation for these fields. Pls help, thanx!
use a regular expression to remove the empty spaces from a string containing the value of the field and compare the length of this to the length of the original string
var str;
str = document.theFormsName.theFieldsName.value;
if (str.replace(/[ ]/g),"".length != str.length)
{
//the field contains spaces
}
for (var ei=0;ei<oneinput.length;ei++)
{
var onechar = oneinput.charAt(ei);
if (space.indexOf(onechar) == -1) return false;
}
return true;
}
This will return true if the string is empty, is a space, a tab, newline, or carriage return. If there are any other characters in the string, it'll return false, which means you'll want to have it process the form. If you want, you can change isEmpty() to isNotEmpty() and switch true and false values in the function.
I was having the same problem as Rachel. The solution posted by trollacious was good, but it only worked for tabs and carriage returns. Blank spaces were still being accepted as valid input. Using the above posts, I was able to build on joe's and troll's ideas and write a function that caught blank spaces as well as tabs, new lines, and cr's. It's pasted below...just thought I would share it.
function isEmptySp(s) {
if (s == null || s.length == 0) return true;
var spaces = /\s+/g;
var exception = /\b/g; //Allows multiple word entries w/spaces in beteween words--i.e. "Emergency Room"
var found = spaces.exec(s);
var keep = exception.exec(s);
if (found && ! keep) {
return true;
}
}
Only a single space...what happens if the user hits the space bar more than once? The function may disallow one space, but accepts two or more as valid input (at least that's what happened when I tried it).
Please don't misunderstand my intentions though...your post was still very helpful.
Rachel just asked for something to check to see if the field had any characters other than space characters. If it does, then it's considered NOT empty. Having only 2 (or 100) spaces will return a true, meaning that the input value IS empty, if that's all it has. The name of the function is isEmpty(), and if the field has ONLY spacing character in it, then it IS EMPTY, and returns true. What you're talking about is completely different. What I wrote for Rachel works in the way she asked for it to work. If you're looking for something to check for an empty string, which means a string that you want to contain characters besides spaces, tabs, newlines, and carriage returns, then this does what you want.
If you're looking for something else, I don't quite understand what you want.
I went back and retested your function and realized I was using var space='\t\n\r'; and NOT var space=' \t\n\r';...I left out the space before \t. Your function catches blank spaces just fine.
I could have saved myself a headache if I would have noticed this last Friday. Oh well...it was good practice anyway.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.