Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Dreamweaver form validation on unique fields

Posted on 2008-06-13
7
Medium Priority
?
572 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 375 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
Independent Software Vendors: 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

Independent Software Vendors: 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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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 dynamically set the form action using jQuery.

721 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