www.webdeveloper.com
Results 1 to 8 of 8

Thread: Validation for Only Alphabets in Javascript

  1. #1
    Join Date
    Dec 2007
    Location
    New Delhi
    Posts
    34

    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 ALPHABETS alone,it should give me alert that is it should not accept ALPHABETS 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..

    Nitin Sharma
    Software Engineer

  2. #2
    Join Date
    May 2006
    Location
    Odenton, MD
    Posts
    1,449
    use regular expression with the test method. on your validation function:
    Code:
    var regexNum = /\d/;
    var regexLetter = /[a-zA-z]/;
    if(!regexNum.test(textbox.value) || !regexLetter.test(textbox.value)){
    alert('Type alphanumeric character');
    return false;
    }
    replace textbox to your textbox pointer.
    Last edited by ZeroKilled; 12-27-2007 at 09:37 PM.
    my mom is javascript, dad is javascripter, granpa is javascriptor, and my little sister is javasRidiculous.
    my nature language is javascript, then come spanish and english -- me

  3. #3
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,373

    Question

    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
    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).

  4. #4
    Join Date
    Dec 2007
    Location
    New Delhi
    Posts
    34
    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

  5. #5
    Join Date
    Dec 2007
    Location
    New Delhi
    Posts
    34
    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...

  6. #6
    Join Date
    Dec 2007
    Location
    New Delhi
    Posts
    34
    Thanks..i found the solution

    the snippet is

    var regexNum = /\d/;
    var regexLetter = /[a-zA-z]/;
    if(!regexNum.test(m) && regexLetter.test(m)){
    alert('Only Alphabets are not allowed..!!');
    return false;
    }



    Note : var m = document.test.employeeid.value;

    Thanks for you Cooperation...

    Nitin Sharma
    Software Engineer

  7. #7
    Join Date
    May 2006
    Location
    Odenton, MD
    Posts
    1,449
    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 ad343df8 and will pass validation even if that isn't how you except string to be. however, improved code follow:

    Code:
    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.
    Last edited by ZeroKilled; 12-27-2007 at 11:35 PM.
    my mom is javascript, dad is javascripter, granpa is javascriptor, and my little sister is javasRidiculous.
    my nature language is javascript, then come spanish and english -- me

  8. #8
    Join Date
    Nov 2007
    Posts
    409
    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/...nd-Number.aspx

    You'll want to update the line
    Code:
    if (!(blankValue) && (fieldContainsANumber && fieldContainsALetter)){
    to something like this:
    Code:
    if (!(blankValue) && (fieldContainsANumber){
    note: this function does not validate that non-alphanumerics are entered (e.g., a user could enter "abc123^" and it would still pass.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles