0

I am putting server side validation but seems it's not working in the way it should. Below is sample code

     //Validation

    private void validation()
    {
        if (txtFName.Text == string.Empty) { Alert("Invalid Name"); return; }

        if (txtLName.Text == string.Empty) { Alert("Invalid Name"); return; }
    }


       // Alert mesage
  public void Alert(string msg)
    {
        ClientScript.RegisterStartupScript(typeof(Page), "SymbolError", "<script
         type='text/javascript'>alert('" + msg + "');</script>");
    }

In my button next click event I am calling this function like

    protected void button_Click(object sender, EventArgs e)
    {
        validation();
    }

Surprisingly, even though I don't enter anything in the texbox (means textbox is empty) ... no alert is coming. Whereas, it should alert.

Can someone point me what I am doing wrong. Appreciate your help.

EDIT:

Most weired thing is that, the same code work fine in other page(s). it alerts fine if the fields are empty or validation failing. Not sure what wrong with this page.

Fe pointer like ... This particular aspx page ... has lot of user controls and those controls ascx page have Javascript. I that could be any issue

Rahul
  • 76,197
  • 13
  • 71
  • 125
  • 2
    Have you considered using the asp.net validation controls? They will simplify this immensely for you. See http://msdn.microsoft.com/en-us/library/ie/debza5t0.aspx. In particular, look at the RequiredFieldValidator - http://msdn.microsoft.com/en-us/library/5hbw267h(v=vs.80).aspx – dash Jan 24 '12 at 22:27
  • Dash ... I know but for somereaon I can't use them ... well not my decision ... I am just a crew member :) – Rahul Jan 24 '12 at 22:29
  • How annoying! Just having to postback to check if a field is empty or not is a pain, especially when the controls are built into the framework. Do you hit a breakpoint in the validation() method when in debug? See http://stackoverflow.com/questions/320999/execute-javascript-function-after-asp-net-postback-without-ajax for some more suggestions! – dash Jan 24 '12 at 22:32
  • Dash ... I am doing other stuff also in btnnext event but have removed them from this post as they are irrelevent ... I am calling validation first ... so that if validation passed then only procede with other function. Yes I did a debug ... it's calling the function and even the Alert function and just passing to the next step. which is weired. – Rahul Jan 24 '12 at 22:34
  • I took a look at some example code we have, and, indeed we are using a ScriptManager control as in http://stackoverflow.com/questions/320999/execute-javascript-function-after-asp-net-postback-without-ajax – dash Jan 24 '12 at 22:36
  • I don't know why you can't use the standard ASP.NET ValidatorControls, as @dash pointed out, but you should indeed reconsider and push for it. It's a bad practice to mix this JavaScript and perform custom server-side validation in the way you have indicated. I've been in a project that used both ASP.NET validators and the same logic in the code-behind. It's a maintenance nightmare and having to fix that becomes a problem, depending on complexity. Push for the validator controls. – Mario J Vargas Jan 24 '12 at 23:23

3 Answers3

0

I just tried following code.

protected void Button1_Click(object sender, EventArgs e)
{
    validation();
}

private void validation() 
{ 
    Alert("Invalid Name"); 
} 


   // Alert mesage 
public void Alert(string msg) 
{ 
    ClientScript.RegisterStartupScript(typeof(Page), "SymbolError", "<script type='text/javascript'>alert('" + msg + "');</script>"); 
} 

seem like everything is working fine. check out this see if there is any settings issue. (not sure i m just doing trial and error).

http://bytes.com/topic/asp-net/answers/518330-clientscript-registerstartupscript

AJP
  • 2,125
  • 3
  • 16
  • 22
0

i sussgest you to use Asp.net Validation controls, such as Required field validator, Compare validator. as the framework will do all the thing for you. then why your going to validate on your own. ?

in aspx

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="InvalidName" ControlToValidate="TextBox1"></asp:RequiredFieldValidator>

if you want to show all your validation in to message box or summary, you can use validation summary control

<asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="True" />
Ravi Gadag
  • 15,735
  • 5
  • 57
  • 83
0

Even with Scriptmanager it didn't worked out ... o, my solution was ... I just changed the validation method a bit and it worked out great ... like below

private bool validation() 
{ 
    if (txtFName.Text == string.Empty) { Alert("Invalid Name"); return false; } 

    if (txtLName.Text == string.Empty) { Alert("Invalid Name"); return false; } 
} 
Rahul
  • 76,197
  • 13
  • 71
  • 125