views:

226

answers:

2

Below is my code:

<asp:TextBox 
  ID="FromDateTextBox" 
  runat="server" />
<asp:ImageButton 
  ID="FromDateImageButton" 
  runat="server" 
  ImageUrl="~/images/calander.png" />
<ajaxkit:CalendarExtender 
  ID="FromDate" 
  runat="server" 
  TargetControlID="FromDateTextBox" 
  CssClass="CalanderControl" 
  PopupButtonID="FromDateImageButton" 
  Enabled="True" />                
<ajaxkit:MaskedEditExtender 
  id="FromDateMaskedEditExtender" 
  runat="server" 
  targetcontrolid="FromDateTextBox" 
  Mask="99/99/9999" 
  messagevalidatortip="true" 
  onfocuscssclass="MaskedEditFocus" 
  oninvalidcssclass="MaskedEditError" 
  masktype="Date" 
  displaymoney="Left" 
  acceptnegative="Left" 
  errortooltipenabled="True" />
<ajaxkit:MaskedEditValidator 
  id="FromDateMaskedEditValidator" 
  runat="server" 
  controlextender="FromDateMaskedEditExtender" 
  controltovalidate="FromDateTextBox" 
  emptyvaluemessage="Date is required" 
  invalidvaluemessage="Date is invalid" 
  display="Dynamic"
  tooltipmessage="Input a date" 
  emptyvalueblurredtext="*" 
  invalidvalueblurredmessage="*"
  validationgroup="MKE" />

I've set Culture="auto" UICulture="auto" in @Page directive and EnableScriptGlobalization="true" EnableScriptLocalization="true" in script manager to have client culture specific date format in my textbox.

I also have a Go button on my page on which I will do a partial post back. So, I want to validate the FromDateTextBox in javascript when the Go button is clicked.

UPDATE

I know how to create a javascript click handler. But because masked editor is already validating the date on focus shift, I'm thinking there should be some boolean property (like IsValid) exposed by it which will allow me to see if the text box contains valid date.

FURTHER TRIALS

I also tried below code and Page_Validators[f].isvalid always returns true even when the date is invalid and MaskEditValidator shows me a red star near the Text box.

function isDateValid() {
  var b = true;              
  for (var f = 0; f < Page_Validators.length; f++) {
    if (!Page_Validators[f].isvalid)
      b = false;
  }
  return b;
}
$('#GoButton').click(function() {

  if (!isDateValid()) {
    return false;
  }
  loadProducts();
});
A: 

Sorry do you mean that your MaskedEditValidator is not working when clicking GO button? Or if you want to write some js to check the date yourself? In this case you can add a client onclick event to GO button. One example in code behind is:

goButton.Attributes["onclick"] = "if (!CheckDate()) return false;";

And in the page file add a javascript function:

function CheckDate()
{
    //check the date here
    return isValid;
}

Now if the date is not valid,the CheckDate() will return false, the goButton event will also return without posting back.

Danny Chen
Oh! I'm sorry.I know how to create a javascript click handler. But because masked editor is already validating the date on focus shift, I was thinking there should be some boolean property (like IsValid) exposed by it which will allow me to see if the text box contains valid date.
Ismail
If your maskedValidator is working fine,clicking GO will not cause a post back if IsValid=false;
Danny Chen
IsValid is which control's property?
Ismail
It's a property of the page. I'm not talking about the code behind but the JS code generated by .NET. When you click GO, a ValidateForm() will check if the page is valid(by the validators) before __doPostBack() is called. If some fields are not valid, this page will not post back.
Danny Chen
IsValid is always returning false. I have load of other controls on my page i.e on other tabs. I'm using TabContainer of AjaxControlToolkit.
Ismail
Is there any other way?
Ismail
You can see my answer how I resolved it. Let me know if you have any better solution.
Ismail
A: 

I resolved it. Basically when there is an invalid date entered in the textbox the MaskedEditExtender changes the provided (OnInvalidCssClass="MaskedEditError") class of the textbox and I picked it up as a checking point for validation.

$('#GoButton').click(function () {
  if($('#<%=FromDateTextBox.ClientID%>').hasClass('MaskedEditError')) {
    return false;
  }
}
Ismail