Home Validate form data
Reply: 0

Validate form data

user11494
1#
user11494 Published in September 21, 2018, 2:58 am

This question already has an answer here:

  • jQuery Validate remote method usage to check if username already exists 2 answers

I have a form that that is already validated by jquery.validate.min.js, what I want is another validation method, ajax call to mysql to check if the email address is already in my DB. How do I merge the validations? I already played a little with the codes but can't figure it out.

My form: http://demos.creative-tim.com/wizard-demo-register?_ga=2.138099576.979789193.1518540669-1813170823.1518540669

My validation code:

$('document').ready(function() {
  var email_state = false;
  $('#email').on('blur', function() {
    var email = $('#email').val();
    if (email == '') {
      email_state = false;
      return;
    }
    $.ajax({
      url: 'index.php',
      type: 'post',
      data: {
        'email_check': 1,
        'email': email,
      },
      success: function(response) {
        if (response == 'taken') {
          email_state = false;
          alert('email is taken');
        } else if (response == 'not_taken') {
          email_state = true;
          alert('email available');
        }
      }
    });
  });
});

The PHP who process the email check:

        <?php
      $db = mysqli_connect('localhost', 'user', 'pass', 'subscribers');
      if (isset($_POST['email_check'])) {
        $email = $_POST['email'];
        $sql = "SELECT * FROM subscribers WHERE email='$email'";
        $results = mysqli_query($db, $sql);
        if (mysqli_num_rows($results) > 0) {
          echo "taken";
        }else{
          echo 'not_taken';
        }
        exit();
      }
      ?>
share|improve this question

marked as duplicate by Sparky jquery-validate StackExchange.ready(function() { if (StackExchange.options.isMobile) return; $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() { var $hover = $(this).addClass('hover-bound'), $msg = $hover.siblings('.dupe-hammer-message'); $hover.hover( function() { $hover.showInfoMessage('', { messageElement: $msg.clone().show(), transient: false, position: { my: 'bottom left', at: 'top center', offsetTop: -7 }, dismissable: false, relativeToBody: true }); }, function() { StackExchange.helpers.removeMessages(); } ); }); }); Feb 14 at 17:10

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • It can be done on your server, returning a response, do you have that implemented already? – bntzio Feb 14 at 0:33
  • Yes i do, i already tried doing it myself but i don't know exactly where the function should be placed. Sometimes the Next button just stops responding or the JS breaks.. – Lugifah Feb 14 at 0:44
  • Or how can i do it more simply, without needing to merge the scripts? – Lugifah Feb 14 at 1:00
  • Are you using express? or which framework are you using? – bntzio Feb 14 at 1:02
  • Oh I see, you're using a php server, can you add your api endpoint code in your question so I can take a look? – bntzio Feb 14 at 1:06

1 Answer 1

active oldest votes
up vote 2 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.365481 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO