Solved

javascript validation using cfform

Posted on 2009-05-06
5
415 Views
Last Modified: 2013-12-24
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

0
Comment
Question by:COwebmaster
  • 3
5 Comments
 
LVL 36

Accepted Solution

by:
SidFishes earned 250 total points
ID: 24319897
just chnage your script to this

<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;}
                        else
                  {
                        return true;                        
                }

        }
</script>
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 24320376
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

0
 
LVL 63

Assisted Solution

by:Zvonko
Zvonko earned 250 total points
ID: 24320404
This two extra lines brings more convenience:
<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.");
                          theForm.confirmPassword.select();
                          theForm.confirmPassword.focus();
                          return false;
                        }
                }
                return true;
        }
</script>
<cfform name="changepwd" action="" method="POST" onsubmit="return validatePassword(_CF_this)">

Open in new window

0
 

Author Comment

by:COwebmaster
ID: 24339914
how can I also ensure that the password field is equal or more than 6 characters using cfform?
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 24342783
How can we ensure this question will not get additional questions appended?
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to redirect https://www to non-www? 6 76
Coldfusion Datefield problem 2 121
How to switch email hosts (part 2) 11 70
Stream live video from Raspberry Pi camera 22 192
One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question