50 users online (0 members and 50 guests)  


  Results 1 to 2 of 2

Related

  1. required fields in a form    Forum: HTML Forum
    Replies: 17
  2. HTML Form with required fields    Forum: HTML Forum
    Replies: 10
  3. Replies: 1
  4. Replies: 3
  1. #1
    zippypic's Avatar
    New User

    Status
    Offline
    Join Date
    Nov 2010
    Posts
    2

    Required Form Fields

    I have written some code that checks whether all the required fields in a form are filled. The idea is to have a single function that can check any number of fields because the form name and required fields are passed to it. Can someone check my code and see if I am making a mistake somewhere?

    in the head of the page:

    Code:
    function validateRequiredFields(FormName,FieldList)
    {
    var BadList = new Array();
    for(var i = 0; i < FieldList.length; i++) {
    	var value = eval('document.' + FormName + '.' + FieldList[i] + '.value');
    	value = stripSpacesFromEnds(s);
    	if(value.length < 1) { BadList.push(FieldList[i]); }
    	}
    if(BadList.length < 1) { return true; }
    var ess = new String();
    if(BadList.length > 1) { ess = 's'; }
    var message = new String('\n\nThe following field' + ess + ' are required:\n');
    for(i = 0; i < BadList.length; i++) { message += '\n' + BadList[i]; }
    alert(message);
    return false;
    }
    in the body:

    Code:
    <form name="login" onSubmit="return validateRequiredFields('login', ['e-mail','password']);" action="../SitePages/Authorization.php" method="post">
                            <table valign="middle" align="center">
                               	<tr>
                                	<td>e-mail:</td>
                                    <td><input type="text" name="e-mail" /></td>
                                </tr>
                                <tr>
                                	<td>Password:</td>
                                    <td><input type="password" name="password" /></td>
                                </tr>
                                <tr>
                                    <td colspan="2"><input type="submit" /></td>
                                </tr>
                            </table>
    						</form>
    I have tried to inserting an alert in the first line of the function and it still does not work which implies that the function is never called.

    Any help will be greatly appreciated, thank you.

  2. #2
    zippypic's Avatar
    New User

    Status
    Offline
    Join Date
    Nov 2010
    Posts
    2

    Re: Required Form Fields

    Sorry, I figured it out, e-mail was an invalid name because of the hyphen.