www.webdeveloper.com
Results 1 to 3 of 3

Thread: Javascript validation

  1. #1
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    51

    Javascript validation

    Hi,

    i have a Javascript validation code like this...


    function validateForms()
    {
    var elementsForms;


    elementsForms = document.getElementsByTagName("form");
    for (var intCounter = 0; intCounter < elementsForms.length; intCounter++)
    {
    return validateForm(elementsForms[intCounter]);
    }
    }

    function validateForm(currentForm)
    {
    var blnvalidate = true;
    var elementsInputs;
    var flag = 0;
    elementsInputs = currentForm.getElementsByTagName("input");
    dropdownelem = currentForm.getElementsByTagName("select");
    textareaelem = currentForm.getElementsByTagName("textarea");

    for (var intCounter = 0; intCounter < elementsInputs.length; intCounter++)
    {
    if (elementsInputs[intCounter].className == "reqquestion")
    { if(elementsInputs[intCounter].type=="text" && elementsInputs[intCounter].value=="")
    {





    alert("Text box should not be empty");
    return false;
    }

    else if(elementsInputs[intCounter].type=="checkbox" && elementsInputs[intCounter].checked==true)

    {
    flag++;



    }




    }


    }
    if(flag==0)
    {
    alert("You should select atleast one");
    return false;
    }


    /* For dropdown*/

    for(var d=0;d<dropdownelem.length;d++)
    {

    if (dropdownelem[d].className == "reqquestion" && dropdownelem[d].selectedIndex==0)

    {
    alert("Please select one value from drop down list")
    return false;


    }



    }





    /* For textarea*/

    for(var t=0;t<textareaelem.length;t++)
    {

    if (textareaelem[t].className == "reqquestion")

    {
    if(textareaelem[t].value=="")
    {

    alert("Text area field is mandatory for this form")

    }
    }



    }






    }

    function validateEmail(elementsInputs, intCounter)
    {
    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!emailFilter.test(elementsInputs[intCounter].value))
    {
    return true;
    }
    }


    function applyOnSubmitToForms()
    {
    elementsForms = document.getElementsByTagName("form");
    for (var intCounter = 0; intCounter < elementsForms.length; intCounter++)
    {
    elementsForms[intCounter].onsubmit = function ()
    {
    if (!validateForms())
    {
    return false;
    }
    }
    }
    }
    function addLoadEvent(func)
    {
    var oldonload = window.onload;
    if (typeof window.onload != 'function')
    {
    window.onload = func;
    }
    else
    {
    window.onload = function()
    {
    oldonload();
    func();
    }
    }
    }

    addLoadEvent(applyOnSubmitToForms);


    And this is the html code for textarea

    <textarea rows="20" cols="30" class="reqquestion" id="form1" name="text2">


    </textarea>




    But the textarea validation is not happening here...
    It would be great if anyone can help me in this.....Plz...

  2. #2
    Join Date
    Nov 2006
    Location
    Oakland
    Posts
    500
    Avoid leaving any blank space between the opening and closing of the textarea tags:
    <textarea rows="20" cols="30" class="reqquestion" id="form1" name="text2"></textarea>
    Any character in between will be considered as part of the value of that field.

  3. #3
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    51

    Javascript Validation

    Hey Thanks a lot dude for helping me....

    It a new information for me. I never thought about it.. I really appreciate your help....

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