www.webdeveloper.com
Results 1 to 4 of 4

Thread: Validating a Textarea

  1. #1
    Join Date
    Oct 2006
    Posts
    33

    Validating a Textarea

    how do i validate a textarea field. This is my current code to validate a text field but it does not work for textarea:
    Code:
    this.validate_element = function(type, validate_element)
        {
            var element = document.getElementsByName(validate_element)[0];
           
            switch(type)
            {
                case "text":
                    if(validate_element)
                    {
                        if(element.value)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    break;
               
                default:
                    break;
            }
           
            return false;
        }
    can anyone tell my why this will not work on textarea? my guess that that i don't access the data with .value but how do i access the data?

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    textarea does not have a type attribute so that would not work.
    use the default in the switch to check for
    Code:
    if(element.nodeName=='TEXTAREA') {
    //validate textarea
    }
    At least 98% of internet users' DNA is identical to that of chimpanzees

  3. #3
    Join Date
    Oct 2006
    Posts
    33
    The type variable is not something i am getting from the DOM but something the programmer(me) has to put in. I would do something like this in the code:

    form_helper.validate_element("textarea", this);

    to validate the current element on a keyup or some other event. I just want to know how would i access the data inside the textarea. I am basically going to add a case for each type i need to check so i would add a case for textarea, select, radio, etc... The reason i am doing it like this instead of automatically getting what type is if the field is a email field, they can do:

    form_helper.validate_element("email", this);

    and i will have a case "email" and in there there will be a regex or something to make sure the entered text is a valid email format.
    Last edited by Eridius; 06-21-2007 at 09:42 AM.

  4. #4
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    If this is pointing to the textarea then this.value would be the data.
    The function you gave would, of course, not work.
    At least 98% of internet users' DNA is identical to that of chimpanzees

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