Link to home
Start Free TrialLog in
Avatar of Mike Waller
Mike WallerFlag for United States of America

asked on

javascript validation using cfform

I have the following but it doesn't work properly.  It still submits.  it should pull up the alert but not submit the form data.  any ideas?
<script language="JavaScript" type="text/javascript">
	function validatePassword() {
		{
			if (document.changepwd.newPassword.value != document.changepwd.confirmPassword.value)
			alert("Your new passwords must match. Please try again.");
			return false;
		}
return true;
	}
</script>
 
<cfform name="changepwd" action="" method="POST" onsubmit="validatePassword()">
	<div class="">Username: <cfinput type="Text" name="username" validateat="onSubmit" message="You must enter a valid username." validate="email" autosuggestminlength="8" required="Yes" visible="Yes" enabled="Yes" showautosuggestloadingicon="True" typeahead="No" maxlength="50"></div>
	<div class="">New password: <cfinput type="Text" name="newPassword" validateat="onSubmit" message="You must enter a valid password." maxlength="16" required="Yes" visible="Yes" enabled="Yes" showautosuggestloadingicon="True" typeahead="No"></div>
	<div class="">Confirm password: <cfinput type="Text" name="confirmPassword" validateat="onSubmit" message="You must enter a valid password." maxlength="16" required="Yes" visible="Yes" enabled="Yes" showautosuggestloadingicon="True" typeahead="No" onchange="javascript:window.alert(form.newPassword.value + ' ' + form.confirmPassword.value);"></div>
	<cfif isDefined("form.submit")>
	do something..
	</cfif>
</cfform>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of SidFishes
SidFishes
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
check this:
<script language="JavaScript" type="text/javascript">
        function validatePassword(theForm) {
                {
                        if (theForm.newPassword.value != theForm.confirmPassword.value){
                          alert("Your new passwords must match. Please try again.");
                          return false;
                        }
                }
                return true;
        }
</script>
<cfform name="changepwd" action="" method="POST" onsubmit="return validatePassword(_CF_this)">
        <div class="">Username: <cfinput type="Text" name="username" validateat="onSubmit" message="You must enter a valid username." validate="email" autosuggestminlength="8" required="Yes" visible="Yes" enabled="Yes" showautosuggestloadingicon="True" typeahead="No" maxlength="50"></div>
        <div class="">New password: <cfinput type="Text" name="newPassword" validateat="onSubmit" message="You must enter a valid password." maxlength="16" required="Yes" visible="Yes" enabled="Yes" showautosuggestloadingicon="True" typeahead="No"></div>
        <div class="">Confirm password: <cfinput type="Text" name="confirmPassword" validateat="onSubmit" message="You must enter a valid password." maxlength="16" required="Yes" visible="Yes" enabled="Yes" showautosuggestloadingicon="True" typeahead="No" ></div>
        <cfif isDefined("form.submit")>
        do something..
        </cfif>
<input type="submit" >
</cfform>

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike Waller

ASKER

how can I also ensure that the password field is equal or more than 6 characters using cfform?
How can we ensure this question will not get additional questions appended?