OnClick doesn't work after Jquery validations - javascript

I have given validations through jquery which works fine on button click.
But as soon as I fill all the data into the page, my onClick does not fires. I dont know why. Please suggest.
Here is my jquery code:-
var ErrArr = [];
$(document).ready(function () {
$('#btnSave').click(function (e) {
e.preventDefault();
validateTitle();
validateddl();
validateTextBoxes();
if (ErrArr.length > 0) {
alert(ErrArr.join("\n"));
ErrArr = [];
return false;
}
});
function validateTitle() {
if ($("#ddlTitle").val() > "0") {
if ($("#ddlTitle").val() == "1104" && $("#txtTitle").val() === "") {
ErrArr.push("Please enter the text in other title");
}
} else {
ErrArr.push('Please select the title');
}
}
function validateTextBoxes() {
if ($("#txt_emp_fname").val() === "") {
ErrArr.push('First name is required');
}
if ($("#txt_emp_mname").val() === "") {
ErrArr.push('Middle name is required');
}
if ($("#txt_emp_lname").val() === "") {
ErrArr.push('Last name is required');
}
if ($("#txtDateofJoin").val() === "") {
ErrArr.push('Date of joining is required');
}
if ($("#txtReligion").val() === "") {
ErrArr.push('Religion is required');
}
if ($("#txtBloodGroup").val() === "") {
ErrArr.push('Blood group is required');
}
if ($("#txtPersonalEmail").val() === "") {
ErrArr.push('Email Id is required');
}
if ($("#txtDtOfBirth").val() === "") {
ErrArr.push('Date of birth is required');
}
if ($("#txtAt").val() === "") {
ErrArr.push('Place of birth is required');
}
if ($("#txtTaluka").val() === "") {
ErrArr.push('Taluka is required');
}
if ($("#txtPostOffice").val() === "") {
ErrArr.push('Post office is required');
}
if ($("#txtDistrict").val() === "") {
ErrArr.push('District is required');
}
if ($("#txtStatePersonal").val() === "") {
ErrArr.push('State is required');
}
if ($("#txtDtMarriage").val() === "") {
ErrArr.push('Date of Marriage is required');
}
if ($("#TxtPassportNo").val() === "") {
ErrArr.push('Passport no is required');
}
if ($("#txtIdMark").val() === "") {
ErrArr.push('Identification mark is required');
}
}
function validateddl() {
if ($("#ddlGender").val === "" || $("#ddlGender").val() == "0") {
ErrArr.push('Please select the gender');
}
if ($("#ddlMaritalStatus").val === "" || $("#ddlMaritalStatus").val() == "0") {
ErrArr.push('Please select the Marital Status');
}
if ($("#ddlNationality").val === "" || $("#ddlNationality").val() == "0") {
ErrArr.push('Please select the Nationality');
}
}
});
Also see my aspx of button:-
<asp:Button ID="btnSave" CssClass="button" Text="Save" runat="server"
CausesValidation="true" onclick="btnSave_Click"
/>

I suggest you do NOT use the click of the button but the submit of the form to validate:
$(document).ready(function () {
$('#formID').on("submit",function (e) {
// e.preventDefault(); // does not belong here
validateTitle();
validateddl();
validateTextBoxes();
if (ErrArr.length > 0) {
e.preventDefault();// but here
alert(ErrArr.join("\n"));
ErrArr = [];
}
});
.
.
.
I assume that also means you should make your button a submit button and make sure the asp does not add a click event since it is already assigned by the jQuery

$('#btnSave').on("click", function(e) {
//e.preventDefault();
validateTitle();
validateddl();
validateTextBoxes();
if(ErrArr.length > 0) {
e.preventDefault(); //use e.preventDefault here
alert(ErrArr.join("\n"));
ErrArr = [];
//return false;
}
});

move event prevent on the button click handler only when client validation fails.
$('#btnSave').click(function (e) {
validateTitle();
validateddl();
validateTextBoxes();
if (ErrArr.length > 0) {
alert(ErrArr.join("\n"));
ErrArr = [];
e.preventDefault();
return false;
}
});

Try this
$('#btnSave').click(function(e) {
//e.preventDefault();
validateTitle();
validateddl();
validateTextBoxes();
if(ErrArr.length > 0) {
e.preventDefault(); //use e.preventDefault here
alert(ErrArr.join("\n"));
ErrArr = [];
//return false;
}
});

Related

Nested jquery "if else if" crashes

I have this block of javascript which is working just fine.
$('#<%=Button_PayFees.ClientID%>').click(function (evt) {
var valuefirstname = $('#<%=TextBox_FirstName.ClientID%>').val().toUpperCase();
var valuelastname = $('#<%=TextBox_LastName.ClientID%>').val().toUpperCase();
var valueaddress = $('#<%=TextBox_Address.ClientID%>').val().toUpperCase();
var valuecity = $('#<%=TextBox_City.ClientID%>').val().toUpperCase();
var valuestate = $('#<%=Dropdownlist_States.ClientID%>').val().toUpperCase();
var valuezipcode = $('#<%=TextBox_ZipCode.ClientID%>').val().toUpperCase();
var valuephone = $('#<%=TextBox_Phone.ClientID%>').val().toUpperCase();
var valueEmail = $('#<%=TextBox_EmailAddress.ClientID%>').val().toUpperCase();
var valuecreditcard = $('#<%=DropDownList_CreditCard.ClientID%>').val();
var valuecreditcardnumber = $('#<%=TextBox_CreditCard.ClientID%>').val().toUpperCase();
var valuecsvcode = $('#<%=TextBox_CSVCode.ClientID%>').val().toUpperCase();
var valueparticipantfirstname = $('#<%=TextBox_ParticipantFirstName.ClientID%>').val().toUpperCase();
var valueparticipantlastname = $('#<%=TextBox_ParticipantLastName.ClientID%>').val().toUpperCase();
var valueTeamName = $("#<%=DropDownList_CheerLevel.ClientID%>").find("option:selected").text();
var valueOrganization = $('#<%=TextBox_Organization.ClientID%>').val().toUpperCase();
var valuedeliveryfirstname = $('#<%=TextBox_DeliveryFirstName.ClientID%>').val().toUpperCase();
var valuedeliverylastname = $('#<%=TextBox_DeliveryLastName.ClientID%>').val().toUpperCase();
var valuedeliveryaddress = $('#<%=TextBox_DeliveryAddress.ClientID%>').val().toUpperCase();
var valuedeliverycity = $('#<%=TextBox_DeliveryCity.ClientID%>').val().toUpperCase();
var valuedeliverystate = $('#<%=DropDownList_DeliveryStates.ClientID%>').val().toUpperCase();
var valuedeliveryzipcode = $('#<%=TextBox_DeliveryZipCode.ClientID%>').val().toUpperCase();
if(valueparticipantfirstname == '' ){
alert('Participant firstname is required.');
evt.preventDefault();
$('#<%=TextBox_ParticipantFirstName.ClientID%>').focus();
}
else if (valueparticipantlastname == ''){
alert('Participant lastname is required.');
evt.preventDefault();
$('#<%=TextBox_ParticipantLastName.ClientID%>').focus();
}
else if (valueOrganization == '') {
alert('Organization is required.');
evt.preventDefault();
$('#<%=TextBox_Organization.ClientID%>').focus();
}
else if (valueTeamName == 'Select Cheer Level') {
alert('Select Cheer Level from the drop down list.');
evt.preventDefault();
$('#<%=DropDownList_CheerLevel.ClientID%>').focus();
}
else if (selection == 'Select DVD Option') {
alert('Please select a DVD option from the drop down list.');
evt.preventDefault();
$('#<%=DropDownList_Options.ClientID%>').focus();
}
else if (valuefirstname == '') {
alert('Firstname is required.');
evt.preventDefault();
$('#<%=TextBox_FirstName.ClientID%>').focus();
}
else if (valuelastname == ''){
alert('Lastname is required.');
evt.preventDefault();
$('#<%=TextBox_LastName.ClientID%>').focus();
}
else if(valueaddress == '') {
alert('Address is required.');
evt.preventDefault();
$('#<%=TextBox_Address.ClientID%>').focus();
}
else if(valuecity == '') {
alert('City is required.');
evt.preventDefault();
$('#<%=TextBox_City.ClientID%>').focus();
}
else if(valuestate == 'SELECT STATE') {
alert('Select state from the drop down list.');
evt.preventDefault();
$('#<%=DropDownList_States.ClientID%>').focus();
}
else if(valuezipcode == '') {
alert('Zip code is required.');
evt.preventDefault();
$('#<%=TextBox_ZipCode.ClientID%>').focus();
}
else if(valuephone == '') {
alert('Phone is required.');
evt.preventDefault();
$('#<%=TextBox_Phone.ClientID%>').focus();
}
else if(valueEmail == '') {
alert('Email Address is required.');
evt.preventDefault();
$('#<%=TextBox_EmailAddress.ClientID%>').focus();
}
else if(valuecreditcard == 'SELECT CARD TYPE') {
alert('Select credit card type from the drop down list.');
evt.preventDefault();
$('#<%=DropDownList_CreditCard.ClientID%>').focus();
}
else if(valuecreditcardnumber == '') {
alert('Enter credit card number.');
evt.preventDefault();
$('#<%=TextBox_CreditCard.ClientID%>').focus();
}
else if(valuecsvcode == '') {
alert('Enter credit card ID code.');
$('#<%=TextBox_CSVCode.ClientID%>').focus();
evt.preventDefault();
}
});
When I change it to add checks for delivery information when a checkbox is checked it crashes ... no idea why as I'm not getting any feed back from the site. The code that crashes is (note the nested else if):
$('#<%=Button_PayFees.ClientID%>').click(function (evt) {
var valuefirstname = $('#<%=TextBox_FirstName.ClientID%>').val().toUpperCase();
var valuelastname = $('#<%=TextBox_LastName.ClientID%>').val().toUpperCase();
var valueaddress = $('#<%=TextBox_Address.ClientID%>').val().toUpperCase();
var valuecity = $('#<%=TextBox_City.ClientID%>').val().toUpperCase();
var valuestate = $('#<%=Dropdownlist_States.ClientID%>').val().toUpperCase();
var valuezipcode = $('#<%=TextBox_ZipCode.ClientID%>').val().toUpperCase();
var valuephone = $('#<%=TextBox_Phone.ClientID%>').val().toUpperCase();
var valueEmail = $('#<%=TextBox_EmailAddress.ClientID%>').val().toUpperCase();
var valuecreditcard = $('#<%=DropDownList_CreditCard.ClientID%>').val();
var valuecreditcardnumber = $('#<%=TextBox_CreditCard.ClientID%>').val().toUpperCase();
var valuecsvcode = $('#<%=TextBox_CSVCode.ClientID%>').val().toUpperCase();
var valueparticipantfirstname = $('#<%=TextBox_ParticipantFirstName.ClientID%>').val().toUpperCase();
var valueparticipantlastname = $('#<%=TextBox_ParticipantLastName.ClientID%>').val().toUpperCase();
var valueTeamName = $("#<%=DropDownList_CheerLevel.ClientID%>").find("option:selected").text();
var valueOrganization = $('#<%=TextBox_Organization.ClientID%>').val().toUpperCase();
var valuedeliveryfirstname = $('#<%=TextBox_DeliveryFirstName.ClientID%>').val().toUpperCase();
var valuedeliverylastname = $('#<%=TextBox_DeliveryLastName.ClientID%>').val().toUpperCase();
var valuedeliveryaddress = $('#<%=TextBox_DeliveryAddress.ClientID%>').val().toUpperCase();
var valuedeliverycity = $('#<%=TextBox_DeliveryCity.ClientID%>').val().toUpperCase();
var valuedeliverystate = $('#<%=DropDownList_DeliveryStates.ClientID%>').val().toUpperCase();
var valuedeliveryzipcode = $('#<%=TextBox_DeliveryZipCode.ClientID%>').val().toUpperCase();
if(valueparticipantfirstname == '' ){
alert('Participant firstname is required.');
evt.preventDefault();
$('#<%=TextBox_ParticipantFirstName.ClientID%>').focus();
}
else if (valueparticipantlastname == ''){
alert('Participant lastname is required.');
evt.preventDefault();
$('#<%=TextBox_ParticipantLastName.ClientID%>').focus();
}
else if (valueOrganization == '') {
alert('Organization is required.');
evt.preventDefault();
$('#<%=TextBox_Organization.ClientID%>').focus();
}
else if (valueTeamName == 'Select Cheer Level') {
alert('Select Cheer Level from the drop down list.');
evt.preventDefault();
$('#<%=DropDownList_CheerLevel.ClientID%>').focus();
}
else if (selection == 'Select DVD Option') {
alert('Please select a DVD option from the drop down list.');
evt.preventDefault();
$('#<%=DropDownList_Options.ClientID%>').focus();
}
else if (valuefirstname == '') {
alert('Firstname is required.');
evt.preventDefault();
$('#<%=TextBox_FirstName.ClientID%>').focus();
}
else if (valuelastname == ''){
alert('Lastname is required.');
evt.preventDefault();
$('#<%=TextBox_LastName.ClientID%>').focus();
}
else if(valueaddress == '') {
alert('Address is required.');
evt.preventDefault();
$('#<%=TextBox_Address.ClientID%>').focus();
}
else if(valuecity == '') {
alert('City is required.');
evt.preventDefault();
$('#<%=TextBox_City.ClientID%>').focus();
}
else if(valuestate == 'SELECT STATE') {
alert('Select state from the drop down list.');
evt.preventDefault();
$('#<%=DropDownList_States.ClientID%>').focus();
}
else if(valuezipcode == '') {
alert('Zip code is required.');
evt.preventDefault();
$('#<%=TextBox_ZipCode.ClientID%>').focus();
}
else if(valuephone == '') {
alert('Phone is required.');
evt.preventDefault();
$('#<%=TextBox_Phone.ClientID%>').focus();
}
else if(valueEmail == '') {
alert('Email Address is required.');
evt.preventDefault();
$('#<%=TextBox_EmailAddress.ClientID%>').focus();
}
else if($('#<%= Checkbox_Delivery.ClientID %>').is(':checked')) {
else if (valuedeliveryfirstname == '') {
alert('Please enter the first name of the person the DVD is being delivered to.');
evt.preventDefault();
$('#<%=TextBox_DeliveryFirstName.ClientID%>').focus();
}
else if (valuedeliverylastname == '') {
alert('Please enter the last name of the person the DVD is being delivered to.');
evt.preventDefault();
$('#<%=TextBox_DeliveryLastName.ClientID%>').focus();
}
else if (valuedeliveryaddress == '') {
alert('Please enter the delivery Address.');
evt.preventDefault();
$('#<%=TextBox_DeliveryAddress.ClientID%>').focus();
}
else if (valuedeliverycity == '') {
alert('Please enter the delivery city.');
evt.preventDefault();
$('#<%=TextBox_DeliveryCity.ClientID%>').focus();
}
else if (valuedeliverystate == 'SELECT STATE') {
alert('Select the delivery state from the drop down list.');
evt.preventDefault();
$('#<%=DropDownList_DeliveryStates.ClientID%>').focus();
}
else if (valuedeliveryzipcode == '') {
alert('Please enter the delivery Zip code.');
evt.preventDefault();
$('#<%=TextBox_DeliveryZipCode.ClientID%>').focus();
}
}
else if(valuecreditcard == 'SELECT CARD TYPE') {
alert('Select credit card type from the drop down list.');
evt.preventDefault();
$('#<%=DropDownList_CreditCard.ClientID%>').focus();
}
else if(valuecreditcardnumber == '') {
alert('Enter credit card number.');
evt.preventDefault();
$('#<%=TextBox_CreditCard.ClientID%>').focus();
}
else if(valuecsvcode == '') {
alert('Enter credit card ID code.');
$('#<%=TextBox_CSVCode.ClientID%>').focus();
evt.preventDefault();
}
});
something in the nested else if causes the code to crash. I can't see it.
This doesn't look right!
else if($('#<%= Checkbox_Delivery.ClientID %>').is(':checked')) {
else if (valuedeliveryfirstname == '') {
The indented else if should just be if there is no preceeding if inside the block.
else if($('#<%= Checkbox_Delivery.ClientID %>').is(':checked')) {
if (valuedeliveryfirstname == '') {
First of all, you should not be using else if for all the other fields otherwise the if loop will exit when it enters the first matching condition. It should be:
if(valueparticipantfirstname == '' ){
alert('Participant firstname is required.');
evt.preventDefault();
$('#<%=TextBox_ParticipantFirstName.ClientID%>').focus();
}
if (valueparticipantlastname == ''){
alert('Participant lastname is required.');
evt.preventDefault();
$('#<%=TextBox_ParticipantLastName.ClientID%>').focus();
}
if (valueOrganization == '') {
alert('Organization is required.');
evt.preventDefault();
$('#<%=TextBox_Organization.ClientID%>').focus();
}
if (valueTeamName == 'Select Cheer Level') {
alert('Select Cheer Level from the drop down list.');
evt.preventDefault();
$('#<%=DropDownList_CheerLevel.ClientID%>').focus();
}
.
.
.
Also, this code:
else if($('#<%= Checkbox_Delivery.ClientID %>').is(':checked')) {
else if (valuedeliveryfirstname == '') {
needs to be changed to:
else if($('#<%= Checkbox_Delivery.ClientID %>').is(':checked')) {
if (valuedeliveryfirstname == '') {
.
.
.

Showing multiple validation alert on button click

I have written a Jquery code for validation. But the problem here is that, First the regular expression message fires and after that requiredField error message fires.
Here is my code:-
var ErrArr = [];
$(document).ready(function () {
$('#btnSave').click(function (e) {
e.preventDefault();
validateTextBoxes();
function FunValidatePan()
if (ErrArr.length > 0)
{
alert(ErrArr.join("\n"));
ErrArr = [];
return false;
}
});
function validateTextBoxes() {
if ($("#txtPanNo").val() === "") {
ErrArr.push('Pan No is required');
}
}
function FunValidatePan() {
var StrPriError = "";
if (Trim(document.getElementById("txtPanNo").value) != "" && Trim(document.getElementById("txtPanNo").value) != "NULL") {
var fil = /^[a-zA-Z0-9]+$/;
if (fil.test(document.getElementById("txtPanNo").value)) {
var abc = Trim(document.getElementById("txtPanNo").value);
if (abc.length != 10) {
StrPriError += ' Enter valid PAN Card\n';
}
}
else {
StrPriError += ' Enter valid PAN Card\n';
}
}
if (StrPriError != "") {
alert(StrPriError);
return false;
}
else {
return true;
}
}
I want that in single message. How to achieve that. Please suggest. Also I want that in Jquery
UPDATE
ASPX
<asp:TextBox ID="txtPanNo" runat="server" Width="100" MaxLength="10"></asp:TextBox>
Please check changes in the above code.
var ErrArr = [];
$(document).ready(function () {
$('#btnSave').click(function (e) {
e.preventDefault();
validateTextBoxes();
FunValidatePan();
if (ErrArr.length > 0)
{
alert(ErrArr.join("\n"));
ErrArr = [];
return false;
}
});
function validateTextBoxes() {
if ($("#txtPanNo").val() === "") {
ErrArr.push('Pan No is required');
}
}
function FunValidatePan() {
if (Trim(document.getElementById("txtPanNo").value) != "" && Trim(document.getElementById("txtPanNo").value) != "NULL") {
var fil = /^[a-zA-Z0-9]+$/;
if (fil.test(document.getElementById("txtPanNo").value)) {
var abc = Trim(document.getElementById("txtPanNo").value);
if (abc.length != 10) {
ErrArr.push('Enter valid PAN Card');
}
}
else {
ErrArr.push('Enter valid PAN Card');
}
}
}

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>) error not working

I am using some javascript code for validations which are as followed:-
var ErrArr = [];
$(document).ready(function () {
$('#btnSave').click(function (e) {
e.preventDefault();
validateTitle();
validateddl();
validateTextBoxes();
checkBoxChecking();
if (ErrArr.length > 0) {
alert(ErrArr.join("\n"));
ErrArr = [];
return false;
}
});
function checkBoxChecking() {
if ($("#chkDeclaration").is(":checked")) {
// alert("first button checked");
return false;
}
else {
ErrArr.push('Check the declaration');
return true;
}
}
function validateTitle() {
if ($("#ddlTitle").val() > "0") {
if ($("#ddlTitle").val() == "1104" && $("#txtTitle").val() === "") {
ErrArr.push("Please enter the text in other title");
}
} else {
ErrArr.push('Please select the title');
}
}
function validateTextBoxes() {
if ($("#txt_emp_fname").val() === "") {
ErrArr.push('First name is required');
}
if ($("#txt_emp_mname").val() === "") {
ErrArr.push('Middle name is required');
}
if ($("#txt_emp_lname").val() === "") {
ErrArr.push('Last name is required');
}
if ($("#txtFatherName").val() === "") {
ErrArr.push('Father name is required');
}
if ($("#txtDateofJoin").val() === "") {
ErrArr.push('Date of joining is required');
}
if ($("#txtReligion").val() === "") {
ErrArr.push('Religion is required');
}
if ($("#txtPersonalEmail").val() === "") {
ErrArr.push('Email Id is required');
}
if ($("#txtDtOfBirth").val() === "") {
ErrArr.push('Date of birth is required');
}
if ($("#txtAt").val() === "") {
ErrArr.push('Place of birth is required');
}
if ($("#txtTaluka").val() === "") {
ErrArr.push('Taluka is required');
}
if ($("#txtPostOffice").val() === "") {
ErrArr.push('Post office is required');
}
if ($("#txtDistrict").val() === "") {
ErrArr.push('District is required');
}
if ($("#txtStatePersonal").val() === "") {
ErrArr.push('State is required');
}
if ($("#txtDtMarriage").val() === "") {
ErrArr.push('Date of Marriage is required');
}
if ($("#TxtPassportNo").val() === "") {
ErrArr.push('Passport no is required');
}
if ($("#txtIdMark").val() === "") {
ErrArr.push('Identification mark is required');
}
}
function validateddl() {
if ($("#ddlGender").val === "" || $("#ddlGender").val() == "0") {
ErrArr.push('Please select the gender');
}
if ($("#ddlMaritalStatus").val === "" || $("#ddlMaritalStatus").val() == "0") {
ErrArr.push('Please select the Marital Status');
}
if ($("#ddlNationality").val === "" || $("#ddlNationality").val() == "0") {
ErrArr.push('Please select the Nationality');
}
}
});
What happens here is, when I use this javascript code, my button click event does not fires. And when I remove the JS code, I get error as
The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
Here is my aspx code:-
<asp:Button ID="btnSave" CssClass="button" Text="Save" runat="server" CausesValidation="true"
OnClick="btnSave_Click" />
Please note, I have searched and tried from other links available from stack and google but I couldn't succeed.
Kindly help
You should see this and use the <asp:Button OnClickClick='..' /> property :
See this or this SO post for more.
<asp:Button
ID="btnSave"
CssClass="button"
Text="Save"
runat="server"
CausesValidation="true"
OnClick="btnSave_Click"
OnClientClick='return performValidation();'
/>
performValidation will be a master function to call that will run first, you can call your validation functions from within it. You should return false if you don't want to page to be posted.
Eg:
First modify each validation function to return false when the validation fails:
$(document).ready(function () {
function checkBoxChecking() {
if ($("#chkDeclaration").is(":checked")) {
return false;
}
else {
ErrArr.push('Check the declaration');
return false; //return false when any validation fails.
}
}
function validateTitle() {
if ($("#ddlTitle").val() > "0") {
if ($("#ddlTitle").val() == "1104" && $("#txtTitle").val() === "") {
ErrArr.push("Please enter the text in other title");
}
} else {
ErrArr.push('Please select the title');
return false;//same here. return false when fail.
}
}
//this is your main validation function..
function performValidation()
{
var ret = true;
ret = checkBoxChecking();
ret = validateTitle();
//etc
//then ..
return ret;
}
});

Add and remove and add a class for multiple inputs with the same class

Im trying to remove a class with the same input class. Its basically a form validation, but I have a dynamic form with inputs that can be added and deleted. So with those dynamic fields, im trying to add and delete classes depending on a submit.
This is what I have:
var i_id = $("#i_id");
var choosec = $("#choose_c");
$('#submit').click(function(e){
if (choosec.val() == "none"){
choosec.addClass("inputerror");
$("#c-error").removeClass("hide-error");
$("#c-error").addClass("show-error");
e.preventDefault();
} else {
choosec.removeClass("inputerror");
$("#c-error").removeClass("show-error");
$("#c-error").addClass("hide-error");
}
if (i_id.val() == "" || i_id.val().length < 7){
i_id.addClass("inputerror");
$("#i-error").removeClass("hide-error");
$("#i-error").addClass("show-error");
e.preventDefault();
} else {
i_id.removeClass("inputerror");
$("#i-error").removeClass("show-error");
$("#i-error").addClass("hide-error");
}
$(".qty").each(function(){
if ($(this).val() == "" && $(this).is(':enabled')){
$(this).addClass("inputerror");
$("#qty-error").removeClass("hide-error");
$("#qty-error").addClass("show-error");
e.preventDefault();
} else {
$(this).removeClass("inputerror");
$("#qty-error").removeClass("show-error");
$("#qty-error").addClass("hide-error");
}
});
$(".name").each(function(){
if ($(this).val() == "" && $(this).is(':enabled')){
$(this).addClass("inputerror");
$("#name-error").removeClass("hide-error");
$("#name-error").addClass("show-error");
e.preventDefault();
} else {
$(this).removeClass("inputerror");
$("#name-error").removeClass("show-error");
$("#name-error").addClass("hide-error");
}
});
$(".price").each(function(){
if ($(this).val() == "" && $(this).is(':enabled') || $(this).val() == "0" && $(this).is(':enabled')){
$(this).addClass("inputerror");
$("#price-error").removeClass("hide-error");
$("#price-error").addClass("show-error");
e.preventDefault();
} else {
$(this).removeClass("inputerror");
$("#price-error").removeClass("show-error");
$("#price-error").addClass("hide-error");
}
});
$(".subtotal").each(function(){
if ($(this).val() == "" && $(this).is(':enabled') || $(this).val() == "0.00" && $(this).is(':enabled')){
$(this).addClass("inputerror");
$("#sub-error").removeClass("hide-error");
$("#sub-error").addClass("show-error");
e.preventDefault();
} else {
$(this).removeClass("inputerror");
$("#sub-error").removeClass("show-error");
$("#sub-error").addClass("hide-error");
}
});
});
for the qty, name, price and subtotal class. The errors aren't showing. Any ideas?
This variable? Have you declare this?
chooseclient.removeClass("inputerror");

Javascript Focus Is Not Working on Aspx Page

Hy Guys,
Please Look at the code and Try to Help Out. The function ive written is not working but its RUNNING properly. Its about To set focus on next content on page im using it on an ASPX page. Heres my code Below :
function SetFocusOnSave(CTag,NTag)
{
alert('Running'+CTag+NTag);
var CurrentTag=document.getElementById(CTag);
var NextTag = document.getElementById(NTag);
if ( (event.keyCode==13)||(event.keyCode==9) )
{
if(CurrentTag.value=="")
{
alert("Please Enter Detail First");
CurrentTag.focus();
}
if(CurrentTag.value!="")
{
event.returnValue=true;
document.getElementById(NextTag).focus();
}
}
}
snametxt.Attributes.Add("onkeypress",
SetFocusOnSave('<%=snametxt.ClientID%>','<%=sdesctxt.ClientID%>');");
have you tried to replace
document.getElementById(NextTag).focus();
with
NextTag.focus();
?
You have to add return false; after you found the false in validation otherwise the flow will continue till end.
Try this function:
function SetFocusOnSave(CTag, NTag) {
alert('Running' + CTag + NTag);
var CurrentTag = document.getElementById(CTag);
var NextTag = document.getElementById(NTag);
if ((event.keyCode == 13) || (event.keyCode == 9))
{
if (CurrentTag.value == "")
{
alert("Please Enter Detail First");
CurrentTag.focus();
return false;
}
if (CurrentTag.value != "") {
event.returnValue = true;
NextTag.focus();
return false;
}
}
};
Hy Guys Ive Tried A NEW CODE AND Fortunately Its Working Fine Here its my Code
function Navigation(CTag, NTag, Number) {
var CurrentTag = document.getElementById(CTag);
var NextTag = document.getElementById(NTag);
var IsNumber = Number; //Checking if value is number
if (NextTag.disabled == true) {
NextTag.disabled = false;
NextTag.className = "txt";
}
if (event.keyCode == 9) {
CurrentTag.focus();
event.returnvalue = false;
}
if (event.keyCode != 9) {
if (event.keyCode == 13) {
if (IsNumber == "Y") {
if (NextTag.disabled == true) {
NextTag.disabled = false;
}
if (CurrentTag.value != "") {
NextTag.focus();
event.returnvalue = true;
}
if (CurrentTag.value == "") {
alert('Please Enter Value To Proceed Further.');
CurrentTag.focus();
event.returnvalue = false;
}
if (isNaN(CurrentTag.value)) {
alert("Please Enter A Valid Number");
CurrentTag.value = "";
CurrentTag.focus();
}
}
if (IsNumber == "N") {
if (CurrentTag.value == "") {
alert('Please Enter Value To Proceed Further.');
CurrentTag.focus();
event.returnvalue = false;
}
if (CurrentTag.value != "") {
NextTag.focus();
event.returnvalue = true;
}
}
}
}
};
Thanks ya'll !! :)

Categories

Resources