Hi,
I have a textbox who has CausesValidation set to True, TextChange event enabled with corresponding eventhandler in the ServerSide Code, and also a CustomValidator code in client Side JavaScript
<asp:textbox id="txtCustZipcode" size="10" tabindex="9" runat="server"
MaxLength="10" AutoPostBack="true" OnTextChanged="txtCustZipcode_TextChanged"
CausesValidation="True"></asp:textbox>
<asp:CustomValidator ID="cstValidate_txtCustZipCode" runat="server"
ClientValidationFunction="fnzipcodeValidation" ControlToValidate="txtCustZipcode"
SetFocusOnError="True"></asp:CustomValidator>
function fnzipcodeValidation(source, args)
{
var strZipCodeValidator = /[&"#+>]/;
args.IsValid=true;
if(document.forms[0].txtCustZipcode.value!='')
{
if (strZipCodeValidator.test(trim(document.forms[0].txtCustZipcode.value)))
{
alert('ZipCode cannot take &,#,+,> and double quotes, Pls correct');
document.forms[0].txtCustZipcode.focus()
document.forms[0].txtCustZipcode.select()
args.IsValid=false;
return false;
}
}
}
The thing is, even if the args.IsValid=false, the Server Side TextChanged event occurs.
Okay, I tried this on the Server Side EventHandler:
Protected Sub txtCustZipcode_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCustZipcode.TextChanged
cstValidate_txtCustZipCode.Validate()
If cstValidate_txtCustZipCode.IsValid Then
End If
End Sub
This works fine, but the trouble with this approach is: did you notice the alert message on the fnZipCodeValidation??? That fires almost four times!
Any clever, simple solution to over come this???