Advertisement
Guest User

Form Validator

a guest
Oct 20th, 2016
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     return function(){
  3.         let userRegex = /^[a-zA-Z0-9]{3,20}$/;
  4.         let passwordRegex = /^[a-zA-Z0-9_]{5,15}$/;
  5.         let emailRegex = /(@)([ -~]+)(\.)/;
  6.         let companyShitReg = /\b[1-9][1-9][1-9][1-9]\b/;
  7.         $("#registerForm").submit(function(e) {
  8.             e.preventDefault();
  9.         });
  10.         $('input#company').click(function () {
  11.             if($('#company').attr('checked','checked')){
  12.                 $('#companyInfo').attr('style','display-inline');
  13.             }
  14.             if($('#company').not(':checked').length){
  15.                 $('#companyInfo').attr('style','display:none');
  16.             }
  17.         });
  18.         $('#submit').click(function () {
  19.             let counter = 0;
  20.             let username = $('input#username').val();
  21.             let password = $('input#password').val();
  22.             let confirmPassword = $('input#confirm-password').val();
  23.             let email = $('input#email').val();
  24.             let compNumber = $('input#companyNumber').val();
  25.             if(password != confirmPassword){
  26.                 $('input#confirm-password').attr('style','border-color: red;');
  27.                 $('input#password').attr('style','border-color: red;');
  28.                 counter++;
  29.             }
  30.             else {
  31.                 $('input#confirm-password').removeAttr('style');
  32.                 $('input#password').removeAttr('style');
  33.                 counter = 0;
  34.             }
  35.             if(!username.match(userRegex)){$('input#username').attr('style','border-color: red;');}
  36.             else {$('input#username').removeAttr('style');}
  37.             if(!password.match(passwordRegex)){$('input#password').attr('style','border-color: red;')}
  38.             else {
  39.                 if(counter == 0) {
  40.                     $('input#password').removeAttr('style');
  41.                 }
  42.             }
  43.             if(!confirmPassword.match(passwordRegex)){$('input#confirm-password').attr('style','border-color: red;');}
  44.             else {
  45.                 if(counter == 0) {
  46.                     $('input#confirm-password').removeAttr('style');
  47.                 }
  48.             }
  49.             if(!email.match(emailRegex)){$('input#email').attr('style','border-color: red;');}
  50.             else {$('input#email').removeAttr('style');}
  51.             if($('#company').is(':checked')){
  52.                 if(!compNumber.match(companyShitReg)){$('input#companyNumber').attr('style','border-color: red;');}
  53.                 else{$('input#companyNumber').removeAttr('style');}
  54.             }
  55.             if(!$('input#confirm-password').is('[style]')
  56.                 && !$('input#username').is('[style]')
  57.                 && !$('input#password').is('[style]')
  58.                 && !$('input#email').is('[style]')
  59.                 && !$('input#companyNumber').is('[style]'))$('#valid').attr('style','display-inline');
  60.             else $('#valid').attr('style','display:none');
  61.         })
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement