Help with form validation

Last post 09-06-2008 8:11 AM by NC01. 6 replies.

Sort Posts:

  • Help with form validation

    07-26-2007, 3:35 PM
    • Loading...
    • XanaLyoko
    • Joined on 07-03-2007, 4:07 PM
    • Posts 68

    In order to validate that all of teh items on my page have been filled out correctly I use these functions:

    function check_form( form )
    {
        var required_fields = check_fields( form );
    
        if ( required_fields > 0 ) 
        {
            alert("Please complete all fields with red labels.");
            return false;
        }
    
        return true;
        
    }
    
    /*
        Check each of the entries for valid input. 
        
        Returns the number of required fields left to fill in.
    */
    function check_fields( form )
    {
        var need_input = 0;
    alert("hey");
        need_input += checkForNull( form.types, 'type_lbl');
        need_input += checkForNull( form.severity, 'severity_lbl');
        need_input += checkForNull( form.parent_system, 'par_lbl');
        need_input += checkForNull( form.system, 'sys_lbl');
        need_input += checkForNull( form.headline, 'head_lbl');
        need_input += checkForNull( form.description, 'desc_lbl');        
    alert("again");
        return need_input;
    
    }
    
    /*
        When an element is changed, if it is a valid value, decrement the number 
            or required fields.
    
        When an element is changed, if it is not a valid value, increment the number 
            or required fields.        
    */
    function checkForNull( element, id  )
    {
        var label = document.getElementById( id );
    alert("hmm");
        if ( element.value == "NULL" || element.value.replace(/\s*$/, '') == "" ) 
        {
            label.className = "incomplete";
            return 1;
        }
    
        label.className = "complete";
        return 0;
        
    }

    The problem right now is that with the alerts to test the form it is never fireing alert("again") and I don't see a reason why it is not.  Also I have found that though it will fire alert("hmm") when I move it into the if statement it stops firing it.

    I can even submit my form without filling in anything and it does not alert that I need to fill in all of the elements.  Does anyone have any ideas?

    Thanks in advance,

    Xana

    If history is to change let it change. If the world is to be destroyed so be it. If it is my fate to die ... I must simply laugh.
  • Re: Help with form validation

    07-26-2007, 7:59 PM

    Hi There,

    1) if ( required_fields > 0 ) should be if ( required_fields.length > 0 )

    2) Is there any javascript error shown ?

    3) Are you working with masterpage or the control is inside another server control (e.g. panel, placeholder and etc), it certainly affect the id after it render to html)

    4) Could you post the complete code? this save replier time to test your scenario locally.

    DC517
    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved.
  • Re: Help with form validation

    07-27-2007, 9:54 AM
    • Loading...
    • XanaLyoko
    • Joined on 07-03-2007, 4:07 PM
    • Posts 68

    Required fields is simply the need_input that is returned, meaning it starts at zero and can go up to 6.

    There is no JavaScript error, even in firefox.  For some reason it simply wont go into the if loop in check for null if the form has been submitted, it will go into when it is called by an on change event.

    I am not working with a masterpage and none of the controls is inside another server control.

    The code for the drop down menus is all generted dynamically from asp but I will provide some examples of the fields that I need to get validated.(see next post)

    If history is to change let it change. If the world is to be destroyed so be it. If it is my fate to die ... I must simply laugh.
  • Re: Help with form validation

    07-27-2007, 9:57 AM
    • Loading...
    • XanaLyoko
    • Joined on 07-03-2007, 4:07 PM
    • Posts 68

    just fixed

    If history is to change let it change. If the world is to be destroyed so be it. If it is my fate to die ... I must simply laugh.
  • Re: Help with form validation

    07-27-2007, 10:47 AM
    • Loading...
    • XanaLyoko
    • Joined on 07-03-2007, 4:07 PM
    • Posts 68

    Firefox just threw out two errors which are the same:
    element has no properties.

    The line in question would be the line that is (if element.value=="NULL" || element.value.replace(/\s*$/, ' ' ) = = " " ).  Why would it be putting out that error as I can alert to the value of the element when I do an onchange command.  If this is causing it not to validate properly how would I fix it?

    If history is to change let it change. If the world is to be destroyed so be it. If it is my fate to die ... I must simply laugh.
  • Client Side validation in server controle

    09-06-2008, 2:53 AM
    • Loading...
    • great1
    • Joined on 09-06-2008, 5:38 AM
    • Posts 8

    its good example

    www.javascript101.blogspot.com

    I AM AZEEM
  • Re: Client Side validation in server controle

    09-06-2008, 8:11 AM
    • Loading...
    • NC01
    • Joined on 08-26-2005, 3:33 PM
    • Posts 9,418
    • TrustedFriends-MVPs

    great1:

    its good example

    www.javascript101.blogspot.com

    Again, this has nothing to do with the original post. Please quit trying to advertise your blog site. This forum is NOT for that.

    NC...

     

Page 1 of 1 (7 items)
Microsoft Communities
Page view counter