Custom validator Question

Posted by Graceosull under ASP.NET on 9/18/2009 | Views : 1383 | Status : [Member] | Replies : 1
Hello,

hope someone can help with this question.

I am working in visual Studio 2005

On my page I have 2 asp textboxes (textbox1 & textbox2), a button and custom validator to validate the data in textbox2. When the button is clicked I call a javascript function to check it data has been entered in textbox1 and if so data must also be entered in textbox2. My problem is that when the button is clicked, the custom validator does not seem to fire and the page is posted back to the server even though the format of the data in textbox 2 is invalid.

Following are code snipets from my code

On the server I have

Private Overloads Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

cmdSave.Attributes.Add("onclick", "return CheckIfYearRequired();")



On the client I have

function CheckIfYearRequired() //function that called on the onclick of the submit button

{

var OrderNo = igtab_getTabById("webtabAcq").Tabs[1].findControl("txtOrderNo").value;

if (OrderNo != "")

{

var OrderYear = igtab_getTabById("webtabAcq").Tabs[1].findControl("txtOrderYear").value;

if (OrderYear == "")

{

alert("Order Year required when a Order Number specified.");

return false;

}

}

return true; //I tried returning false here and it didn't make a difference

}



function validateOrderYear(args) //custom validator function

{

var OrderYear = igtab_getTabById("webtabAcq").Tabs[1].findControl("txtOrderYear").value;

args.IsValid = false;


if (isNaN(OrderYear) || OrderYear.length<4)

{

alert("Not a valid Order Year. The Year must be numeric and a 4 digit year.");

return;

}

else

{

var theDate = new Date();

var theYear = theDate.getFullYear();

if (OrderYear > theYear || OrderYear == "0000")

{

alert("Not a valid Order Year. The Year must be less than or equal to the current year.");

return;

}

}

args.IsValid = true;

}




<asp:TextBox id="txtOrderYear" style="Z-INDEX: 137; LEFT: 152px; POSITION: absolute; TOP: 56px"

tabIndex="81" runat="server" CssClass="StandardText" Width="72px" MaxLength="4"></asp:TextBox>

<asp:CustomValidator id="cvOrderYear" style="Z-INDEX: 133; LEFT: 176px; POSITION: absolute; TOP: 48px"

runat="server" ControlToValidate="txtOrderYear" ClientValidationFunction="validateOrderYear"></asp:CustomValidator>

Thanks

Kay




Responses

Posted by: Lakhangarg on: 9/20/2009 [Member] [Moderator] Silver

Up
0
Down
Hi-

in the above code you are using return in case of result is not expected.
you should use args.IsValid = false; instead of just return.

Thanks & Regards
Lakhan Pal Garg
http://lakhangarg.blogspot.com/


Graceosull, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response