Note that there are some explanatory texts on larger screens.

plurals
  1. POjquery ui dialog form validation
    text
    copied!<h1>Jquery UI dialog Form :</h1> <p>i have to create registration for a new website</p> <h2>using :</h2> <ol> <li>jquery ui</li> <li>php </li> <li>mysql</li> <li>Html5</li> </ol> <h2>the problem :</h2> <ol> <li>cannot check if user or email exist.</li> <li><strong>check_exist</strong> function return the value of <strong>notexist</strong> and it always <strong>not defined</strong> </li> <li>when is remove the notexist of if (bValid &amp;&amp; notexist) it work well</li> <li>how can check if e mail exist on key up</li> <li>how can disable register button if there's any error</li> </ol> <hr> <h3>ajax.php file source:</h3> <pre><code>require_once '../global.php'; if ($_GET['do'] == "register") { if (isset($_POST['name'])) { $name = strip_tags($_POST['name']); $pass = strip_tags($_POST['password']); $ins = mysql_insert('users', array('u_name' =&gt; $name, 'password' =&gt; $pass)); if ($ins) { $u_id = mysql_insert_id(); $username = strip_tags($_POST['username']); $telephone = strip_tags($_POST['telephone']); $gender = strip_tags($_POST['gender']); $nationality = strip_tags($_POST['nationality']); $email = strip_tags($_POST['email']); $ins2 = mysql_insert('patient_info', array( 'u_no' =&gt; $u_id, 'name' =&gt; $username, 'gender' =&gt; $gender, 'nationality' =&gt; $nationality, 'tel' =&gt; $telephone, 'email' =&gt; $email )); if ($ins2) { echo "Welcome you can login now :)"; } } } } if ($_GET['do'] == "userexist") { $username = $_POST['username']; $result = mysql_query("select * from users where u_name ='" . $username . "'"); $row = mysql_fetch_row($result); if ($row &gt; 0) { echo 1; }else{ echo 0; } } if ($_GET['do'] == "mailexist") { $email = $_POST['email']; $result = mysql_query("select * from patient_info where email ='" . $email . "'"); $row = mysql_fetch_row($result); if ($row &gt; 0) { echo 1; }else{ echo 0; } } </code></pre> <h3>js file source:</h3> <pre><code>$(function() { $("#gender").buttonset(); var info = $('#notfi-info').hide(); $('#notfi-error').hide(); var name = $("#name"), email = $("#email"), password = $("#password"), username = $("#username"), telephone = $("#telephone"), gender = $('#gender input[name=gender]'), nationality = $("#nationality"), allFields = $([]).add(name).add(email).add(password).add(username).add(telephone).add(password).add(nationality), tips = $("#validateTips"); function getradio(r) { if (r.filter(':checked')) { if (r.filter(':checked').val() == "male" || r.filter(':checked').val() == "female") { return r.filter(':checked'); } } else { return false; } } function updateTips(t) { tips.text(t).addClass("ui-state-highlight"); setTimeout(function() { tips.removeClass("ui-state-highlight", 1500); }, 500); } function checkLength(o, n, min, max) { if (o.val().length &gt; max || o.val().length &lt; min) { o.addClass("ui-state-error"); updateTips("Length of " + n + " must be between " + min + " and " + max + "."); return false; } else { return true; } } function checkRegexp(o, regexp, n) { if (!( regexp.test(o.val()) )) { o.addClass("ui-state-error"); updateTips(n); return false; } else { return true; } } function check_exist(x, y, z, k) { jQuery.ajax({ type : "POST", url : "inc/ajax.php?do=" + z, data : y + '=' + x.val(), cache : false, success : function(response) { if (response == "1") { x.addClass("ui-state-error"); updateTips(k); return false; } if (response == "0") { return true; } } }); } function subregform(form) { jQuery.ajax({ type : form.attr("method"), url : form.attr("action"), data : form.serialize(), success : function(msg) { info.find(".info").text(msg); info.toggle(); $(this).dialog("close"); }, error : function() { alert("failure"); } }); } $("#dialog-form").dialog({ autoOpen : false, height : 450, width : 400, modal : true, buttons : { "Create an account" : function() { var bValid = true; allFields.removeClass("ui-state-error"); bValid = bValid &amp;&amp; checkLength(name, "login name", 3, 16); bValid = bValid &amp;&amp; checkLength(email, "Email", 6, 80); bValid = bValid &amp;&amp; checkLength(password, "Password", 5, 16); bValid = bValid &amp;&amp; checkLength(username, "Username", 5, 16); bValid = bValid &amp;&amp; checkLength(telephone, "Telephone", 7, 13); // bValid = bValid &amp;&amp; checkLength(getradio, "Gender", 4, 5); bValid = bValid &amp;&amp; checkLength(nationality, "Nationality", 3, 25); bValid = bValid &amp;&amp; checkRegexp(telephone, /^[0-9]([0-9])+$/i, "Telephone may consist of 0-9 only."); // bValid = bValid &amp;&amp; checkRegexp(name, /^[a-z]([0-9a-z_])+$/i, "Username may consist of a-z, 0-9, underscores, begin with a letter."); // From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/ bValid = bValid &amp;&amp; checkRegexp(email, /^((([a-z]|\d|[!#\$%&amp;'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&amp;'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. ui@jquery.com"); // bValid = bValid &amp;&amp; checkRegexp(password, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9"); var notexist = true; notexist = notexist &amp;&amp; check_exist(name, 'username', 'userexist', 'Login Name Already Exist!'); notexist = notexist &amp;&amp; check_exist(email, 'email', 'mailexist', 'E-Mail Already Exist!'); if (bValid &amp;&amp; notexist) { var form = $('#dialog-form form'); subregform(form); // $("#users tbody").append("&lt;tr&gt;" + "&lt;td&gt;" + name.val() + "&lt;/td&gt;" + "&lt;td&gt;" + email.val() + "&lt;/td&gt;" + "&lt;td&gt;" + password.val() + "&lt;/td&gt;" + "&lt;td&gt;" + username.val() + "&lt;/td&gt;" + "&lt;td&gt;" + telephone.val() + "&lt;/td&gt;" + "&lt;td&gt;" + gender.filter(':checked').val() + "&lt;/td&gt;" + "&lt;td&gt;" + nationality.val() + "&lt;/td&gt;" + "&lt;/tr&gt;"); $(this).dialog("close"); } }, Cancel : function() { $(this).dialog("close"); } }, close : function() { allFields.val("").removeClass("ui-state-error"); } }); $("#create-user").button().click(function() { $("#dialog-form").dialog("open"); }); }); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload