I am making a jQuery ajax post in a javascript function used by a asp:CustomValidator
control. The web method returns a boolean. How do I convert the result make the validation script function correctly.
Client side code
<script language="javascript" type="text/javascript">
function ValidateInput(source, args) {
if($('#MyTxtBox').val().length > 0) {
var result;
var webMethod = 'http://domain/webservice.asmx/ValidateInput';
var parameters = "{'input':'" + $('#MyTxtBox').val() + "'}";
$.ajax({
type: "POST",
url: webMethod,
data: parameters,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
args.IsValid = msg.d;
}
});
}
else {
args.IsValid = false;
}
}
</script>
<asp:TextBox ID="MyTxtBox" runat="server" />
<asp:CustomValidator ID="cvCreditCardNumber" runat="server" ClientValidationFunction="ValidateCCNumber" Display="Dynamic"
ErrorMessage=" Please enter valid input." />
Web service code
[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
public bool ValidateInput(string input)
{
if(input.Equal("jQuery is awesome!"))
return true;
else
return false;
}