Solved

javascript validation using cfform

Posted on 2009-05-06
5
406 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In our day to day coding, how many times have we come across a necessity to check whether a URL is a broken link or not? For those of you that answered countless and are using ColdFusion like myself, then this article is for you.  It will show yo…
Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

912 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now