View Full Version : Checking for invalid characters excluding white space...

04-04-2003, 03:51 AM
hi all, i have a text box for users to enter their name....so i'm using javascript to check whether they are entering any invalid characters....is there any way to check for invalid characters excluding white space? becoz some names have a space in between....

04-04-2003, 11:29 AM
function validateMe(field) {
var valid = "\' abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
var ok = "yes";
var temp;
for (var i=0; i<field.value.length; i++) {
temp = "" + field.value.substring(i, i+1);
if (valid.indexOf(temp) == "-1") ok = "no";
if (ok == "no") {
alert("Invalid entry!");

04-04-2003, 12:31 PM
So, pure English alphabet, yes? How about &uuml;, &ouml;, &aring;, &aelig;, &ccedil; for instance?
// Function will return true if legal, false if not.
// It will also return false unless the string contains at least one character.
function fnCheckString(str){
var re=/^[- a-z.']+$/i;
return re.test(str);

04-04-2003, 07:50 PM
Originally posted by liorean
var re=/^[- a-z.']+$/i;
Is that right? Do you get start/end of line markers in text-box string returns?

04-04-2003, 07:54 PM
Do I have to direct YOU to my regex tutorial, too, Brothercake? I thought you would know them well enough :)

You know, ^ matches start of string, not start of line. Same is the case with $, but for ends.

Of course, if you use the m flag, they match start and end of lines, also.

04-04-2003, 08:21 PM
Originally posted by liorean
You know, ^ matches start of string, not start of line. Same is the case with $, but for ends.
[creeps off in embarrassment ...]

04-05-2003, 04:10 AM
thanks for the codes! :thumbsup:

04-06-2003, 01:10 AM
Heheh... and with all my "lording over" (as beetle says (how's my memory beetle? ;))) of regex's you haven't mastered them yet, brothercake?

Shame shame! ;)

Honestly though, I have no idea what I would do without regex's, considering the type of stuff we have to validate at work sometimes. I'd probably go insane (/me points miranda to regex tutorials, especially after looking at the code above! regex's would apparently save you a bit of time too!). A couple of other good tutorials/introductions:



04-06-2003, 01:25 AM
There's also the word boundary flag as well...

04-06-2003, 03:04 AM
The DevEdge Ref and Guide are pretty good resources, but I just can't keep from linking my own article about this:

Original: Regular Expressions in JavaScript (http://evolt.org/article/Regular_Expressions_in_JavaScript/17/36435/) on eVolt.
WA Edition: Programmer's Guide to Regular Expressions (http://www.wsabstract.com/javatutors/redev.shtml) on JavaScript Kit.

Whammy - Well, the word boundary flag wasn't in discussion, even.

04-06-2003, 03:24 AM
I'm all for anyone and everyone learning about regular expressions, but from looking at your articles, they assume a certain amount of technical proficiency, which most of the posters here do not have.

They look fine to me though...