validating text entered in text box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ravindarjobs
    New Member
    • May 2007
    • 86

    validating text entered in text box

    Hi friends,

    i am using .net 2005 but we dont use validation controls.
    my requirement is that i have to limit user entering only alpha values in "first name" and "last name" fields.

    the page has some text box and a submit button.
    on clicking the submit buttion the validation must be done in java script.

    how can i limit user to my requirement?


    thank you
  • ravindarjobs
    New Member
    • May 2007
    • 86

    #2
    i have used this code. It is working a bit fine.

    Code:
     var regexAlpha=/[a-zA-z]/;
        if(!regexAlpha.test(document.getElementById('ctl00_DefaultContent_txtFristName').value.trim()))
        {
            alert('First Name Should Contain only Alphabet values');
            return false;
        }
    suppose if we enter values liks "2345" in the text box then it is giving error. but if we enter text like "sdfa234234 " it is not giving error.

    what changes should i make to it to accept only alphabets with spaces?
    Last edited by acoder; Nov 26 '08, 08:54 AM. Reason: Added [code] tags

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      You need to constrain the whole input:
      Code:
      var regexAlpha=/^[a-z ]+$/i;
      The i makes it case-sensitive to match all cases. ^ and $ are start and end match characters.

      PS. please use [code] tags when posting code.

      Comment

      • ravindarjobs
        New Member
        • May 2007
        • 86

        #4
        thank you Acoder.

        that is a good information.

        belive or unbelieve i was blindly working on these regular expressions for more than a day now and i used to think "$" is used for "space".


        thank you

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          No problem at all.

          For regular expressions, this is a good resource.

          Comment

          Working...