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):
I'm thinking it might be in the regex expression, but not sure, lol.... pretty much bumping this thread..
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.
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?
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.
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.