24 February 2009

Validating using asp.net validation in custom javascript

Wow,
I just found how easy it is to have custom javascript at the same time using ASP.Net validators to validate user input (Client Side).
In my situation, the ASP.Net validators were not firing because I had hooked custom javascript to a button click.
e.g.
function ValidateAddressAddition()
{
if (confirm('Are you sure you wish to add a previous address?'))
{
return true;
}
return false;
}

The above code will block ASP.Net validation but if I add lines in Red, it would work fine
function ValidateAddressAddition()
{
if (Page_ClientValidate())
{
if (confirm('Are you sure you wish to add a previous address?'))
{
return true;
}
}
return false;
}

2 comments:

Bongani said...

i prefer onclientclick="return confirm('are u sure u want to delete');"

Musa said...

Ayikho inkinga Bongani ukuthi iyiphi indlela oyincemelayo. In my case I had ASP.NET validators that were not working because of this. Angikaze ngiyisebenzise lendlela yakho may next time mfwethu.