Bug - Form Validation

Discussion in 'jQuery' started by Jeremy Benson, Nov 14, 2013.

  1. #1
    Hey,

    I'm working with JavaScript for the first time, and seem to be having a bit of trouble with my code. All was going well until my submit button wouldn't do anything. It was working before, so it's probably a good indication that new addition to my code has a bug...

    Sorry for the atrocious code, not too good. Just try to make things work, lol.
    
    $(function() {
    
    $( "#datepicker" ).datepicker();
    
    $('#errorHandle').hide();
    
    });
    
    
    function processForm(){
    
    
    // Set the variables to form data
    
    var userFirstName = $('#userFirstName').val();
    var userLastName = $('#userLastName').val();
    var userDOB = $('#datepicker').val();
    var userCountry = $('#userCountry').val();
    var userCity = $('#userCity').val();
    var userBio = $('#userBio').val();
    var userRegStep = 2;
    
    //Handle errors in form data.
    
    var errors = new Array("0");
    var digitReg = new RegExp("[0-9]");
    var specReg = new RegExp("[!@#$%^&*()-_+={}[]:;\"|\,.<>?/]");
    
    if(digitReg.exec(userFirstName){
    
    errors[0] = "1";
    
    }
    
    if(specReg.exec(userFirstName){
    
    errors[0] = "2";
    
    }
    
    if(digitReg.exec(userLastName){
    
    errors[0] = "1";
    
    }
    
    if(specReg.exec(userLastName){
    
    errors[0] = "2";
    
    }
    
    if(digitReg.exec(userCountry){
    
    errors[0] = "1";
    
    }
    
    if(specReg.exec(userCountry){
    
    errors[0] = "2";
    
    }
    
    if(digitReg.exec(userCity){
    
    errors[0] = "1";
    
    }
    
    if(specReg.exec(userCity){
    
    errors[0] = "2";
    
    }
    
    if(digitReg.exec(userBio){
    
    errors[0] = "1";
    
    }
    
    if(specReg.exec(userBio){
    
    errors[0] = "2";
    
    }
    
    alert(errors[0]);
    
    }
    
    }
    
    
    Code (markup):
     
    Jeremy Benson, Nov 14, 2013 IP
  2. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #2
    I'm thinking it might be in the regex expression, but not sure, lol.... pretty much bumping this thread..
     
    Jeremy Benson, Nov 19, 2013 IP
  3. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #3
    Okay - so you wanna check each submission (entry) for digits and special characters? This looks like an immensly complicated way of doing it, if that's what you're trying to do.
     
    PoPSiCLe, Nov 19, 2013 IP
  4. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #4
    well, I decided to re-do this in php, because I couldn't find the error... for some reason jscript is a bit of a struggle, but I guess it's because it's new....I didn't think it was that much work...each form submission has to be validated, so it's only two checks per field... What other way is there to do it?
     
    Jeremy Benson, Nov 20, 2013 IP
  5. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #5
    Pseudocode:
     
    // declare / set checks (either variables or functions) 
    $error = '' ;
    foreach ($_POST as $key => $value) {
    // do your validation here
    if ($error != '') {
    // output error
    } 
    } 
    
    PHP:
    Would vastly reduce lines of code, and you can easily add specific validation for given fields.

    Would also recommend you look into PHP filter-functions, to avoid reinventing the wheel.
     
    PoPSiCLe, Nov 21, 2013 IP
  6. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #6
    lol, thanks, I actually tried that in another form, but I was making extra calls to some function or other, I guess if I structure the code right it'll be fine.
     
    Jeremy Benson, Nov 21, 2013 IP