/    Sign up×
Community /Pin to ProfileBookmark

validate for numbers and letters

I’m fairly new to Javascript and need a little help. How do I validate a field to ensure it contains at least one alpha character and at least one numeric character? Any help is appreciated.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@CharlesFeb 01.2005 — [font=monospace]<input onchange="if (!/[a-z]/i.test (this.value) || !/d/.test (this.value)) {alert (); this.focus()}" type="text">[/font]
Copy linkTweet thisAlerts:
@7studFeb 01.2005 — The easiest way is to use regular expressions.

tutorial:

http://www.sitepoint.com/article/expressions-javascript

list of symbols:

http://www.siteexperts.com/tips/functions/ts23/page1.asp

Regular expressions have a test() function, which returns true or false depending on whether a given match was found in a given string. For instance, here is a simple regex:

var my_regex = /[a-z]/;

That expression will match any lower case letter in a string. So, you could do this:

var my_regex = /[a-z]/;

var result = my_regex.test("123456a");

That bit on the right of the equals sign says to search the string "123456a" for the pattern stored in the variable my_regex. Since the symbols stored in the variable my_regex will match any lower case letter, if any lower case letter is found in the string, then result will be true, otherwise result will be false.
×

Success!

Help @redsoxrule 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.5,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...