/    Sign up×
Community /Pin to ProfileBookmark

Validation for Only Alphabets in Javascript

Hi All,
Greetings..!!

This is my Frst post in this Forum..So i hope the Perfect Answer…

I am working on ASP,VBSCript,Javascript..

I have a textbox and i want to enter Numerics,alphanumerics in the text box.

If i enter [B]ALPHABETS[/B] alone,it should give me alert that is it should not accept [B]ALPHABETS[/B] alone.

For eg: Michael123,Sachin007,124569,0055989, are all acceptable
but

Michael,Sachin,Rudra,John etc. are not acceptable..
I have tried in number of forums but no one is giving perfect answer.So i expect perfect answer from your side..

Thnaks in Advance..

[B]Nitin Sharma
Software Engineer[/B]

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@ZeroKilledDec 28.2007 — use regular expression with the test method. on your validation function:
<i>
</i>var regexNum = /d/;
var regexLetter = /[a-zA-z]/;
if(!regexNum.test([i]textbox[/i].value) || !regexLetter.test([i]textbox[/i].value)){
alert('Type alphanumeric character');
return false;
}


replace [i]textbox[/i] to your textbox pointer.
Copy linkTweet thisAlerts:
@JMRKERDec 28.2007 — 
If i enter ALPHABETS alone,it should give me alert that is it should not accept ALPHABETS alone.

For eg: Michael123,Sachin007,124569,0055989, are all acceptable
[/quote]

By your request, how do you expect to enter anything that does not start with a number? By entering 'Michael' before you get to the '123' you would get seven errors and rejections before the number sequence could be found.

Your request is impossible to fulfill in its present form if you plan on checking each character as it is entered. I might be possible to check if you look at full word entries (entries between spaces) or word entries followed by a sentinal (say the comma or a RETURN key).
Copy linkTweet thisAlerts:
@nitindelauthorDec 28.2007 — Hi,

Thanks for the Reply...

The snippet that u have sent is for Alphanumeric...but if i enter Numerics it is not accepting...

I repeat again......

If i enter Numerics...or if i enter Alphanumerics... it should accept....


but if i enter only Alphabets like Michael,Sachin,Krishna, then it should give alert that only Alphabets not allowed...

Please tell..

Thanks

Nitin Sharma
Copy linkTweet thisAlerts:
@nitindelauthorDec 28.2007 — Hi,


If i mean the characters entered are captured first..and then checked that the characters entered are not all Alphabets,if all characters found are alphabets then it should throw alert....

Provided it should accept Numeric values wwhen entered alone like 11111,22222,44444, 0005895 etc etc...


It should not accept Michael,Sachin,Nitin...but it can accept Sachin2Sachin, Sachin007nitin, Nitin123,Krishna007Krishna, 123Nitin ,111111,222225,558969,2256987..etc etc...

I have come to know the properties like charAt and Indexof can be used..if u can help i welcome ur help...

I hope you got my requirements now...

Thnaks u so much for you cooperation...i have to subit the module by EOD.

Thanks & Regards

Nitin Sharma

Software Engineer

Thnaks and hoping for reply...
Copy linkTweet thisAlerts:
@nitindelauthorDec 28.2007 — Thanks..i found the solution

the snippet is

[B] var regexNum = /d/;

var regexLetter = /[a-zA-z]/;

if(!regexNum.test(m) && regexLetter.test(m)){

alert('Only Alphabets are not allowed..!!');

return false;

}[/B]



Note : [B]var m = document.test.employeeid.value;[/B]

Thanks for you Cooperation...

Nitin Sharma

Software Engineer
Copy linkTweet thisAlerts:
@ZeroKilledDec 28.2007 — that snippet will not work well. when i coded the script i wasn't thinking very well. i coded it having in mind that you need alphanumeric character without wondering the order in which is typed. so, i can type [b]ad343df8[/b] and will pass validation even if that isn't how you except string to be. however, improved code follow:

<i>
</i>validate = function(txt){
var word = /w+/g;
var compound = /^[A-Z][a-z]+d+$/;
var digit = /^d+$/;
if(txt.match(word).length != 1) throw new Error('More than one phrase isn't allowed');
if(digit.test(txt) || compound.test(txt))return true;
throw new Error('Type character followed by digits or just digits');
}


here is the thing. the first comparison is made to check whether user has typed more than one 'word'. then come the true validation matching your need. be aware that when validation don't meet the requirement, function throw error instead of an alert to user.
Copy linkTweet thisAlerts:
@nap0leonDec 28.2007 — According to your scenario all you really care about is that at least 1 number is entered?

Here is a page I built that validates that a form input contains at least 1 letter and 1 number. It could be easily modified to accept "at least 1 number".

http://www.jasondahlin.com/WebSite2/CodeSnippets/JavaScript-Regular-Expression-Field-Contains-A-Letter-And-Number.aspx

You'll want to update the line
[CODE]
if (!(blankValue) && (fieldContainsANumber && fieldContainsALetter)){
[/CODE]


to something like this:
[CODE]
if (!(blankValue) && (fieldContainsANumber){
[/CODE]


note: this function does not validate that non-alphanumerics are entered (e.g., a user could enter "abc123^" and it would still pass.
×

Success!

Help @nitindel 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.19,
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,
)...