/    Sign up×
Community /Pin to ProfileBookmark

Field validaition – alphanumeric chars and spaces only

I have a field I need to validate to allow only alphanumeric characters, which I have a funciton in place for that works fine, however I need to allow spaces as well in that field as well.

My javascript that validates my field as alphanumeric is below, I thought I could just add a space into the char list, but its not that simple. Please help.

[B]//Validate peoplesoft id
if (document.createPackage.peoplesoftId.value != “”)
{
// allow ONLY alphanumeric keys, no symbols or punctuation
var checkOK = “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789”;
var checkStr = document.createPackage.peoplesoftId.value;
var allValid = true;
for (i = 0; i < checkStr.length; i++)
{
ch = checkStr.charAt(i);
for (j = 0; j < checkOK.length; j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
}
if (!allValid)
{
alert(“Please enter only letter and numeric characters in the “Alias” field.”);
document.createPackage.peoplesoftId.focus();
return (false);
}

}

else
{
alert(“Please enter a PeopleSoft ID.”);
document.createPackage.peoplesoftId.focus();
return(false);
} [/B]

to post a comment
JavaScript

12 Comments(s)

Copy linkTweet thisAlerts:
@flyingvauthorJan 15.2007 — Ok DUH! I figured it out, I just had to add a space within the char set, as opposed to it being at the beginning or end, since spaces aren't interpreted at those spots.

So

[B]"ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz0123456789";[/B]

as opposed to

[B]"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" ;[/B]

Thanks anyways, I'm sure the Javascript gurus will be proud of me solving my own issue once again.
Copy linkTweet thisAlerts:
@mrhooJan 15.2007 — Your code is working too hard. A six character password has to run 63 separate tests for each character.

You could test the string just once, with a regular expression.

[B]allvalid=( /[^A-Za-zd ]/.test(checkStr)==false);[/B]
Copy linkTweet thisAlerts:
@OverstatementJan 16.2007 — Your code is working too hard. A six character password has to run 63 separate tests for each character.

You could test the string just once, with a regular expression.

[B]allvalid=( /[^A-Za-zd ]/.test(checkStr)==false);[/B][/QUOTE]

Regular Expressions can do this and much more!
Copy linkTweet thisAlerts:
@flyingvauthorJan 16.2007 — Ok sure thats more elegant, but I'll bet your regular expressions don't work on the ladies!?

No really, thank you both for the advice and feedback - this was very helpful and insightful. I still have to try it out and break it down.

I'm not a javascript buff so I don't quite understand how it works at a glance. I can see that chars A-Z and a-z are accounted for, but how does it account for numbers and spaces? Can someone break the statement down for me a little?

[B]allvalid=( /[^A-Za-zd ]/.test(checkStr)==false);[/B]

flyingv
Copy linkTweet thisAlerts:
@OverstatementJan 16.2007 — RE claims another victim! I think it was designed to confuse anyone reading it. Anyway, you wanted only letters, numbers and spaces right? Do this:
<i>
</i>/[ws]/

RE can be used for string functions like replace, match and indexOf. Just don't enclose it quotes.
Copy linkTweet thisAlerts:
@mrhooJan 16.2007 — allvalid=( /[^A-Za-zd ]/.test(checkStr)==false);

the regular expression literal is enclosed between the slashes[B] (/.../)[/B]

Square brackets define a set or group of matching characters[B]([...])[/B]

The caret[B] (^) [/B]has two different functions in a regular expression.

Placed at the beginning of the expression, the matching pattern has to begin at the start of the test string.

Inside a bracket[B] ([^) [/B]it means match anything that is NOT in the set.

With this expression, we want to find if there are any illegal characters in the checkStr-If the string contains any characters that are [B]not [/B]upper or lower case letters (A-Za-z), digits (d) or spaces ( )-the last character inside the brackets is a space, by the way.

[B]d[/B] matches the digits 0-9.

the shorthand [B]w[/B] matches A-Za-z0-9 and the underline character (_).

If your allowed characters included underlines as well as digits and alphanumeric characters you could write the expression like this:

[B]allvalid= (/[^w ]/.test(checkStr)==false);[/B]

(note-don't forget the space character at the end- ' ]' )

Remember, we are looking for anything that does [B]not [/B]belong, so we want the test to return false, meaning the string contains only the characters you specified.

You could do a positive test, and check each character by anchoring the match at the beginning(/^) and end($/) of the string:

[B]allvalid=/^[A-Za-zd ]+$/.test(checkStr);[/B]

A valid string returns true in this case- the [B](+)[/B] after the match matches one or more of the items in the group, and the ($) signifies the match must end, as well as begin the string. Everything in the string, beginning to end, has to be one of the characters in the bracketted set.

It can be more efficient to find one bad apple than to certify that all the apples are good, so I tend to use the negative match when I test input strings. But with regular expressions, to each his own.
Copy linkTweet thisAlerts:
@sinciliteJan 16.2007 — RE can be used for string functions like replace, match and indexOf. Just don't enclose it quotes.[/QUOTE]

Can you expand on that a little further? Especially with regard to indexOf?

Thanks
Copy linkTweet thisAlerts:
@OverstatementJan 16.2007 — Umm...it means you can do this:
<i>
</i>var IndexOfFirstSpace = Text.indexOf(/s/);
//or even indirectly
var RE = /[abc]/
var IOFirstABOrC = Text.indexOf(RE);
Copy linkTweet thisAlerts:
@mrhooJan 16.2007 — I use .search in place of .indexOf when I have a regular expression to locate in a string.

var ax='some string'.indexOf(/s/); //returns -1

var ax='some string'.search(/s/); //returns 4
Copy linkTweet thisAlerts:
@OverstatementJan 16.2007 — Never knew Regular Expressions don't work in indexOf. Weridness!
Copy linkTweet thisAlerts:
@mrhooJan 16.2007 — I was curious why, if a regexp wouldn't work in an indexOf, it didn't throw an error. And so I tested all sorts of strings and regexps, looking for any occasion it would find a match.

var str='some string.';

var rx=/some/;

alert(str.indexOf(rx)); returns -1

str='/some/ string.';

alert(str.indexOf(rx)); returns 0

The regexp is converted to a string, literally-

exactly as if you were calling- .indexOf('/some/')
Copy linkTweet thisAlerts:
@sinciliteJan 17.2007 — Ah right thanks for that - will have a bit of a look in to it further

*Sorry OP highjacked your thread abit there!
×

Success!

Help @flyingv spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...