I need help i found this tutorial for validating form without refreshing but the problem is i think this is for just text fields 🙂 but i’m using 4 different radiobuttons i really need this script to work with these radiobutton !
here is the javascript file 🙂 !
runOnLoad(function(){
$("input#name").select().focus();
});
$(function() {
$('.error').hide();
$(".button").click(function() {
// validate and process form here
$('.error').hide();
var name = $("input#name").val();
if (name == "") {
$("label#name_error").show();
$("input#name").focus();
return false;
}
var email = $("input#email").val();
if (email == "") {
$("label#email_error").show();
$("input#email").focus();
return false;
}
var mobile = $("input#mobile").val();
if (mobile == "") {
$("label#mobile_error").show();
$("input#mobile").focus();
return false;
}
var college = $("input#college").val();
if (college == "") {
$("label#college_error").show();
$("input#college").focus();
return false;
}
var university = $("input#university").val();
if (university == "") {
$("label#university_error").show();
$("input#university").focus();
return false;
}
var level = $("input#level").val();
if (level == "") {
$("label#level_error").show();
$("input#level").focus();
return false;
}
var first_preference = $("input#first_preference").val();
if (first_preference == "") {
$("label#first_preference_error").show();
$("input#first_preference").focus();
return false;
}
var second_preference = $("input#second_preference").val();
if (second_preference == "") {
$("label#second_preference_error").show();
$("input#second_preference").focus();
return false;
}
var third_preference = $("input#third_preference").val();
if (third_preference == "") {
$("label#third_preference_error").show();
$("input#third_preference").focus();
return false;
}
var heard = $("input#heard").val();
if (heard == "") {
$("label#heard_error").show();
$("input#heard").focus();
return false;
}
var applying = $("input#applying").val();
if (applying == "") {
$("label#applying_error").show();
$("input#applying").focus();
return false;
}
var strength = $("input#strength").val();
if (strength == "") {
$("label#strength_error").show();
$("input#strength").focus();
return false;
}
var weakness = $("input#weakness").val();
if (weakness == "") {
$("label#weakness_error").show();
$("input#weakness").focus();
return false;
}
var previousEx = $("input#previousEx").val();
if (previousEx == "") {
$("label#previousEx_error").show();
$("input#previousEx").focus();
return false;
}
var dataString = 'name='+ name + '&email=' + email + '&mobile=' + mobile + '&college=' + college + '&university=' + university + '&level=' + level + '&first_preference=' + first_preference + '&second_preference=' + second_preference + '&third_preference=' + third_preference + '&heard=' + heard + '&applying=' + applying + '&strength=' + strength + '&weakness=' + weakness + '&previousEx=' + previousEx;
$.ajax({
type: "POST",
url: "php/database_sorting.php",
data: dataString,
success: function() {
$('#contact_form').html("<div id='message'></div>");
$('#message').html("<h2>Contact Form Submitted!</h2>")
.append("<p>We will be in touch soon.</p>")
.hide()
.fadeIn(1500, function() {
$('#message').append("<img id='checkmark' src='images/done.png' />");
});
}
});
return false;
});
});
Assuming you have a radio buttons grouped by name, to check if one of them is selected:
Fiddle
Another way to “skip” JS validation on radio buttons is to leave one of them pre-checked when you generate the page:
This way one of the radios will always be selected.
Using HTML5, you can use the
requiredattribute oninputelements (except button inputs) to skip JS validation for modern browsers. FiddleFinally, if all your
input/textarea/checkbox/radios are inside of a form, you can use jQuery’s.serialize()to generate a query string instead of building it manually. And if they aren’t inside of a form, you can wrap them inside of one.Fiddle
And as already commented in the question, JS validation is just to provide a better UI (e.g. showing errors without refreshing the page), you need server-side validation if you’re making your site public as JS can be easily bypassed.
One more side-note, assuming your
inputs are inside of a form, it’s better to attach your validation to the form’s.submit()handler than on a button click, to ensure that your validation will be fired whenever the form is submitted independently of browser or user clicking in the submit button or pressing Enter in a text input element.