Check for Duplicates & Validate textbox format

dgraham 09 Jan, 2010
I've got my head around the why and what, can't seem to construct the how:

My form is a single textbox submitting data to database & all results then viewed publicly. Which runs flawlessly.
Now I need to validate, when submitted, that the user has:
Created the proper expression: /^[0-9]{3}[\- ]?[0-9]{3}[\- ]?[0-9]{3}$/; (example xxx-xxx-xxx)
Checked database for duplicate entry and accepted or denied addition

I can't seem to put that all together and any help would be fantastic. I'm like right there dangit.😉
GreyHead 10 Jan, 2010
Hi dgraham,

You can do this either with JavaScript (plus AJAX) in the form itself before submission and/or in the server-side validation box after submission.

The JavaScript validation of the format can be done with a custom LiveValidation; the Database check needs Ajax - there are examples of both in the forums here.

Bob
dgraham 10 Jan, 2010
thanks GreyHead, put me on the right track!
Got the validation down. The DB check for dupes seems to be the tougher nut. Looking...
GreyHead 11 Jan, 2010
Hi dgraham,

Here's a snippet from a development version of the Joomla Registration plugin. It's the code to check an AJAX query for a duplicate username. You can see here the kind of MySQL query that you will need.
    		    $json = stripslashes($_POST['json']);
                $json = json_decode($json);

                if ( isset($json->username) ) {
                    $username = strtolower(trim($json->username));
                    $db =& JFactory::getDBO();
                    $query = "
                        SELECT COUNT(*)
                        	FROM `#__users`
                        	WHERE LOWER(`username`) = ".$db->quote($username).";";
                    $db->setQuery($query);
                    $response = (bool) !$db->loadResult();
                    $response = array('username_ok' => $response );
                    echo json_encode($response);

Bob
dheimoz 24 Feb, 2010
Sorry if I'm simple minded, but where do I need to put this code in order to have it worked?
This topic is locked and no more replies can be posted.