Skip to content

Instantly share code, notes, and snippets.

@erhanyasar
Last active March 9, 2023 14:04
Show Gist options
  • Save erhanyasar/027e0badb14748abc27fe4463fbd44f1 to your computer and use it in GitHub Desktop.
Save erhanyasar/027e0badb14748abc27fe4463fbd44f1 to your computer and use it in GitHub Desktop.
A gist to validate form inputs
$(document).ready(function() {
let f1err = 0,
f2err = 0,
f3err = 0,
date = new Date();
$("#sendBTN").click(function() {
if (($("#username").val() == "") || ($("#phone").val() == "")) {
f1err = 1;
if ($("#username").val() == "") {
$(".username").addClass("has-error");
$(".namesurname_txt").css("display","block");
} else {
$(".phone").addClass("has-error");
$(".phone_txt").css("display","block");
}
} else {
f1err = 0;
if ($("#username").val() != "") {
$(".username").removeClass("has-error");
$(".namesurname_txt").css("display","none");
} else {
$(".phone").removeClass("has-error");
$(".phone_txt").css("display","none");
}
}
if (($("#byear").val() == "") || ((date.getFullYear() - $("#byear").val()) < 18)) {
f3err = 1;
$(".byear").addClass("has-error");
$(".byear_txt").css("display","block");
} else {
f3err = 0;
$(".byear").removeClass("has-error");
$(".byear_txt").css("display","none");
}
if (f1err == 0 && f2err == 0 && f3err == 0) {
window.location = "confirmation.html?username=" + $("#username").val() + "&phone=" + $("#phone").val();
return true;
} else
return false;
});
if ($("#username").exists()) {
let usernametxt = $.urlParam('username').replace("%20", " ");
$("#username").html(usernametxt);
}
if ($("#phone").exists()) {
let phonetxt = $.urlParam2('phone').replace("%20", " ");
$("#phone").html(phonetxt);
}
});
$.urlParam = function(name) {
let results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
return results[1] || 0;
}
$.urlParam2 = function(name) {
var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
return results[1] || 0;
}
jQuery.fn.exists = function() {
return this.length > 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment