www.webdeveloper.com
Results 1 to 2 of 2

Thread: Validating a form with default values

  1. #1
    Join Date
    Jan 2006
    Posts
    3

    Validating a form with default values

    I am trying to validate a form that has default values. Having no luck. The current setup works when no value is entered, but all fields have default values allowing a user to hit submit without modifying the fields. I need it to display the error message if the field value is blank AND if the field value is the default value. hope this makes sense.

    basically if the field is blank it needs the error message to display.
    if the default value(ex: First Name) is left in the field, the error message needs to display.


    Code:
    <script type="text/javascript" language="JavaScript">
    function CheckRequiredFields() {
    var errormessage = new String();
    // Put field checks below this point.
    
    if(WithoutContent(document.form.FirstName.value == ""))
    	{ errormessage += "\n\nPlease enter a First Name."; }
    	
    if(WithoutContent(document.form.LastName.value))
    	{ errormessage += "\n\nPlease enter a Last Name."; }
    
    if(WithoutContent(document.form.DateOfBirth.value))
    	{ errormessage += "\n\nPlease enter an Email Address."; }
    
    if(WithoutContent(document.form.ContactNumber.value))
    	{ errormessage += "\n\nPlease enter a Phone Number."; }
    
    if(WithoutContent(document.form.Email.value))
    	{ errormessage += "\n\nPlease select a Contact For."; }
    
    if(WithoutContent(document.form.GradYear.value))
    	{ errormessage += "\n\nPlease select a Contact For."; }
    
    if(WithoutContent(document.form.School.value))
    	{ errormessage += "\n\nPlease select a Contact For."; }
    
    if(WithoutContent(document.form.CoachName.value))
    	{ errormessage += "\n\nPlease select a Contact For."; }
    
    if(WithoutContent(document.form.CoachContactEmail.value))
    	{ errormessage += "\n\nPlease select a Contact For."; }
    	
    // Put field checks above this point.
    if(errormessage.length > 2) {
    	alert('NOTE:' + errormessage);
    	return false;
    	}
    return true;
    } // end of function CheckRequiredFields()
    
    
    function WithoutContent(ss) {
    if(ss.length > 0) { return false; }
    return true;
    }
    
    function NoneWithContent(ss) {
    for(var i = 0; i < ss.length; i++) {
    	if(ss[i].value.length > 0) { return false; }
    	}
    return true;
    }
    
    function NoneWithCheck(ss) {
    for(var i = 0; i < ss.length; i++) {
    	if(ss[i].checked) { return false; }
    	}
    return true;
    }
    
    function WithoutCheck(ss) {
    if(ss.checked) { return false; }
    return true;
    }
    
    function WithoutSelectionValue(ss) {
    for(var i = 0; i < ss.length; i++) {
    	if(ss[i].selected) {
    		if(ss[i].value.length) { return false; }
    		}
    	}
    return true;
    }
    
    //-->
    </script>
    		  <form action="http://www.domainname.com/MailForm/contact.php" method="post" name="form" class"form_text" onSubmit="return CheckRequiredFields()">
    		<input type="hidden" name="recipient" value="">
    		<input type="hidden" name="redirect" value="">
    		<input type="hidden" name="subject" value="Enquiry">
    		<tr>
                <td align="left" valign="top" colspan="2" style="font-size:16px;">
                Register Now!
                </td>
    		</tr>
            <tr>
            <td>&nbsp;
            
            </td>
            </tr>
    		<tr>
    		<td align="left" valign="top">
    		<input type="text" style="width:200px" name="FirstName" class="form_text" value="First name" onclick="this.value='';">
            </td>
    		</tr>
            <tr>
            	<td height="4"></td>
            </tr>
            <tr>
    		<td align="left" valign="top">
    		<input type="text" style="width:200px" name="LastName" class="form_text" value="Last Name" onclick="this.value='';">
            </td>
            </tr>

  2. #2
    Join Date
    Jun 2007
    Posts
    667
    This should test for either an initially blank field left blank, or a field containing no non-whitespace characters regardless of initial value:
    Code:
    function withoutContent( elem ) 
    {
      return ( elem.value == elem.defaultValue  && !elem.value.length ) || !/\S/.test( elem.value );
    }
    
    // EXAMPLE USAGE:
    
    if( withoutContent( document.form.FirstName ) )
    	{ errormessage += "\n\nPlease enter a First Name."; }

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