Solved

Dreamweaver form validation on unique fields

Posted on 2008-06-13
7
564 Views
Last Modified: 2013-12-13
This is an extension of the question in http://www.experts-exchange.com/Web_Development/WebApplications/Q_23153700.html

Similarly, I have created a form in Dreamweaver CS3 and used their validator functions. I have also created a database table via MySQL on the servers where the email field was set to accept unique values only. I am interested in knowing how to go about validating an UNIQUE email address and display an error message if the email address entered is not unique.

Thanks in advance
function MM_validateForm() { //v4.0

  if (document.getElementById){

    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;

    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);

      if (val) { nm=val.name; if ((val=val.value)!="") {

        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');

          if (p<1 || p==(val.length-1)) errors+='- '+nm+' please enter a valid e-mail address.\n';

        } else if (test!='R') { num = parseFloat(val);

          if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';

          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');

            min=test.substring(8,p); max=test.substring(p+1);

            if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';

      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }

    } if (errors) alert('The following error(s) occurred:\n'+errors);

    document.MM_returnValue = (errors == '');

} }

Open in new window

0
Comment
Question by:tubbycub
  • 4
  • 3
7 Comments
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 21782695
Hi tubbycub,

Checking for the uniqueness of a value in a database cannot be handled by javascript alone.  The easiest way for you to do it would be to set the column in the table to be a unique index.  Any attempt to insert a record will generate a MySQL error and you would then trap the error and provide another message to the user.

The only other way to maybe do it would be to use Spry to autocomplete the field in question.  If the email address already exists in the table, the autocomplete would show the user that fact, but nothing would stop them from submitting.  You would still need the unique index in the table to prevent dupes.
0
 

Author Comment

by:tubbycub
ID: 21782750
Yes, I noticed that if I try to submit an existing value, I will get a blank page with the following words

"Duplicate entry 'dupe value' for key 2"

So, how do I go about trapping the error and provide another message to the user?
0
 
LVL 70

Accepted Solution

by:
Jason C. Levine earned 125 total points
ID: 21782993
If you look at the insert transaction PHP code, you will see a line like this:

>>   $Result = mysql_query($insertSQL, $connSAGES) or die(mysql_error());

that or die(mysql_error()) is the part that controls what happens on error.  Right now, the line tells the script to halt and display the mysql error message.

You can change the part after "or..." to do anything you want.
0
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.

 

Author Comment

by:tubbycub
ID: 21783120
Thanks Jason again for the prompt response.

I get what you were saying but I am not very familiar with the PHP syntax. What I really want from the trapping is either for the browser to load up another html page or better still, simply display a line of text on the same php page.

Would you be kind enough to assist further on this?
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 21783126
I'm actually tied up with paying work at the moment.  I'll try to find you another Expert who is willing to do the coding.
0
 

Author Comment

by:tubbycub
ID: 21784247
No problem, will look forward to your reply soon.
0
 

Author Closing Comment

by:tubbycub
ID: 31467074
Thanks, I've figured out the rest of the codes.
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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

914 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

12 Experts available now in Live!

Get 1:1 Help Now