Solved

Dreamweaver form validation on unique fields

Posted on 2008-06-13
7
563 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

705 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

14 Experts available now in Live!

Get 1:1 Help Now