Wrox Programmer Forums
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 12th, 2004, 02:02 AM
Authorized User
 
Join Date: Jun 2003
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default Form Validation

Hi

I have an online questionnaire with a question and multiple answers with radio butons for participants to select their answer.

I need to validate that ALL questions have been answered, but each questionnaire has varying number of questions and each question has varying number of answers, so the validation has to be dynamic. I want to do this client side with javascript, can anyone point me in the right direction.

Thanks

Andy G

Update

I've got this far but the script wont run - can anyone see what is wrong?

<script language="JavaScript">

function Validate()
{
    var myObject = document.body.getElementsByTagName('input');
    for (i = 0; i < myObject.length; i++)
    {
        if (myObject[i].type=='radio')
        {
                    for (x = 0; x < myObject[i].length; x++)
            {

                if (myObject[i][x].checked)
                {
                    return true;
                }
                else
                {
                    alert('You have not answered all the questions!');
                    return false;
                }
            }
        }
    }
}

</script>
 
Old July 15th, 2004, 02:44 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 141
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to acdsky
Default

Hi

Have a look at this. Its is completely dynamic and you can specify in variables what is required and what not & Numeric etc. In this case I have added a popup to show which values are not entered. I uncommented the alerts. I got this off the net so cheers to that guy:

On the form call this function on when the button is pressed:

<SCRIPT language="JavaScript">
function Validate() {
    // define a variable to hold an error message.
    var strErrorMessage = String("") ;
    var varPassed ;
    var varReasonFailed ;
    // Get a handle to our form object.
    var f = form1 ;
    // Update the value in our hidden field, so that we know what
    // button was pressed.
   // f.hidAction.value = s.name ;
    // Declare which fields are optional.
    f.Reset.optional = true ;
    //n is for the vb split function on required.asp...know how many splits to do??:)
    n = 0;
    // Declare which fields are supposed to be numeric.
    f.ClientTel.numeric = true ;
    f.TotalOppVal.numeric = true ;
    f.DCVal.numeric = true ;
    f.PotRevenue.numeric = true ;
    // Iterate through the elements on our form and validate them
    // as we go.
    for(var i=0; i<=f.elements.length-1; i++) {
        varPassed = "PASSED" ;
        varReasonFailed = "" ;
        // Get a handle to the current element.
        var e = f.elements[i] ;
        // We'll store the value of the element in a variable.
        var ElementValue = "" ;
            // Use a switch statement to work out what type of element
            // we have, because we get the value of different elements
            // in differing ways.
            switch(e.type) {
                case "text":
                    ElementValue = e.value ;
                    break ;
                case "select-one":
                    ElementValue = e[e.selectedIndex].id ;
                    break ;
                case "textarea":
                    ElementValue = e.value ;
                    break ;
            }
        // OK, we're not interested in Hidden Fields,Buttons,
        // or Optional fields so skip them
        if( e.type != "hidden" && e.type != "button" && !e.optional )
        {
            // Once we get here we need to make sure that the user has entered some
            // sort of entry (in other words - make sure that the field isn't empty).
            if ( ElementValue == null || ElementValue == "" ) {
                // If the field is empty then we append a message to our Error Message
                // string.
                strErrorMessage += e.id + ".\n" ;
                varPassed = "FAILED" ;
                n = n + 1;
                varReasonFailed += "Because no value was entered.\n" ;
            }
        }
        // Now validate the fields that are supposed to be numeric.
        if(e.numeric == true) {
            if( isNaN(ElementValue) ) {
                // The user entered a non-numeric value where a
                // numeric value was required.
                strErrorMessage += "Enter a numeric value for " + e.id + ".\n" ;
                varPassed = "FAILED" ;
                n = n + 1;
                varReasonFailed += "Because the value entered was not numeric.\n" ;
            }
        }
        if(e.numeric == false) {
            if( !isNaN(ElementValue) && ElementValue != null && ElementValue != "" ) {
                // The user entered a non-numeric value where a
                // numeric value was required.
                strErrorMessage += "You cannot enter a numeric value for " + e.id + ".\n" ;
                varPassed = "FAILED" ;
                n = n + 1;
                varReasonFailed += "Because the value entered was numeric." ;
            }
        }
        var strDetails = ""
        strDetails += "VALIDATING: " + e.id + "\n"
        strDetails += "-------------------------------------------\n\n"
        strDetails += "TYPE: " + e.type + ",\n"
        strDetails += "VALUE: " + ElementValue + ",\n"
        strDetails += "NUMERIC: " + e.numeric + ",\n"
        strDetails += "OPTIONAL: " + e.optional + ".\n\n"
        strDetails += "This element " + varPassed + " the validation\n"
        strDetails += varReasonFailed ;

        //alert(strDetails) ;
    }

    // If there is an error message, display it to the user, otherwise,
    // submit the form.
    if (strErrorMessage.length >= 1) {
        msgWindow = window.open('','newWin','width=400,height=400,scre enX=400,screenY=400,top=200,left=350');
        //open the window and pass the error and count of errors
        msgWindow.location.href = 'required.asp?Error=' + strErrorMessage + "&Count=" + n ;
    } else {
        f.submit() ;
    }

}

</script>

The Popup

<%
ErrorD = request.QueryString("Error")
i = request.QueryString("Count")
Splt = split(ErrorD,".")
a = 0
%>
<table align="center">
<tr><td bgcolor="#CCCCCC">Required Fields not Entered</td></tr>
<%
for b = 0 TO i
 z = Splt(a)
 %>
 <tr><td align="left"><%response.write(Z)%></td></tr>
 <%
 a = a + 1
next
%>
<tr><td align="center"><input name="Close" align="middle" type="button" value="Close" onClick='window.close()';></td></tr>
</table>

Hope this helps

Regards
Marnus





Similar Threads
Thread Thread Starter Forum Replies Last Post
Standalone validation + web form validation morbo Struts 0 August 19th, 2008 04:02 AM
Form Validation Stanny Access VBA 5 December 20th, 2005 08:13 PM
Validation of Form qazi_nomi Javascript 5 August 2nd, 2004 03:49 PM
Form not refreshing after form validation Mimi Javascript How-To 0 August 25th, 2003 03:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.