Advertisement
tourniquet

AJAX jQuery Validation

Aug 18th, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // newad.html
  2. $(document).ready(function() {
  3.   $('#submitButton').on('click', function() {
  4.     var data = {};
  5.     data.formPhone = $('#formPhone').val();
  6.  
  7.     $.ajax({
  8.       dataType: 'json',
  9.       data: data,
  10.       type: 'POST',
  11.       async: false,
  12.       url: 'http://localhost:3000/result'
  13.     });
  14.   });
  15. });
  16.  
  17.  
  18. // validate script
  19. // form id == new-Ad
  20. $('#new-Ad').validate({
  21.     onkeyup: false,
  22.  
  23.     formPhone: {
  24.     required: true,
  25.     remote: {
  26.         url: '/result',
  27.         type: 'POST'
  28.        }
  29.     }
  30. });
  31.  
  32.  
  33. // routes.js
  34. result: function(req, res) {
  35.     var form = new formidable.IncomingForm();
  36.  
  37.     form.parse(req, function(err, fields) {
  38.         var phone = fields.formPhone;
  39.  
  40.         var query = Ad.find({ 'phone': phone });
  41.  
  42.         query.exec(function(err, num) {
  43.             if (err)
  44.                 throw err;
  45.  
  46.             if (!num.length) {
  47.                 res.status(200).send('true');
  48.             } else if (num.length) {
  49.                 res.status(200).send('false');
  50.             }
  51.         });
  52.     });
  53. },
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement