Solved

Dreamweaver form validation on unique fields

Posted on 2008-06-13
7
568 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
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.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

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.  …
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

808 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