I am using the below code to validate the asp.net page while the button click.i want to check the text box is empty or not. If its empty means it doesn't go to the server side click event. If it has some value means process go to code behind click event.
Now i met small problem. If text box value == "" means it return the error message and go the server side click event. How do i prevent the server side post back when the condition is false.
<asp:Button ID="btn_add" runat="server" Text="Submit" OnClick="add_new" OnClientClick="valid_name();"/>
function valid_name() {
var name = document.getElementById('<%= txt_name.ClientID%>').value;
if (name == "") {
alert('Error Alert : You must enter a valid name !.');
return false;
}
else {
return true;
}
}
RequiredFieldValidator? More info here: w3schools.com/aspnet/control_reqfieldvalidator.asp