Solved

Dreamweaver form validation on unique fields

Posted on 2008-06-13
7
569 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Ahax pagination 9 47
Unexpected End of File 11 54
PHP 7 issue seeing runtime MS SQL driver in PHP info page 5 33
php error 27 55
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
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 …

679 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