Thursday, March 29, 2012

Server Side Validation

Hello,
I have made a custom control for a text field. I have to do server side validation for it. I just want it to see if a password equals a given value, if not validation should fail.
Can someone provide me with some pseudo script as I am new to ASP.NET.
Thanks.In your custom control, create a public property (read only), "IsValidPassword". In IsValidPassword's get portion, check for whether txtPassword.Text equals your value. If it does, return true, else return false.

So it'd look something like

public bool IsValidPassword
{
get
{
if(txtPassword.Text == "mendhak")
{
return true;
}
else
{
return false;
}
}
}
Thanks for your answer. But some confusion still exists. I have the following code and it complies without error.

protected void validatePass_ServerValidate(object source, ServerValidateEventArgs args)
{
if (txtPass.Text != "Mohit")
{ validatePass.IsValid = false;
validatePass.ErrorMessage = "Wrong Password!";

}
else validatePass.IsValid = true;

}
}

But it does not generate an error message when the wrong password is entered. What do I have to do to get an error message to be generated as is is the case with the CLIENT SIDE controls I have made...

Thanks!
Aah, you could've just continued your questions together in this thread rather than create a new thread for it.

Now, you should use args.IsValid = false rather than the name of the validator itself.
Thank you!

That works

My program works as I want it to but I have a theoretical question. If I want an error message to display when args.IsValid = false, for it to be considered SERVER SIDE do I have to include it in my code as follows:

if (txtPass.Text != "Mohit")
{args.IsValid = false;
validatePass.Text = "*";
validatePass.ErrorMessage = "Wrong Password!";


}
else args.IsValid = true;

I ask since I can set the error message in the custom validator control but I am worried if I do, it will be considered server side. I am not sure hence I ask.
Thank you!

That works

My program works as I want it to but I have a theoretical question. If I want an error message to display when args.IsValid = false, for it to be considered SERVER SIDE do I have to include it in my code as follows:

if (txtPass.Text != "Mohit")
{args.IsValid = false;
validatePass.Text = "*";
validatePass.ErrorMessage = "Wrong Password!";


}
else args.IsValid = true;

I ask since I can set the error message in the custom validator control but I am worried if I do, it will be considered server side. I am not sure hence I ask.
I ask since I can set the error message in the custom validator control but I am worried if I do, it will be considered server side. I am not sure hence I ask.

I meant to say it would be considered CLIENT SIDE...;)
If I understand you correctly, which I probably don't, then you fear the .ErrorMessage property, right?

ErrorMessage and Text are properties of your validator. When you set them, the page will show the error message/text as your page is setup. Now, are you asking whether... actually... I don't understand your question. Try rephrasing it?
If I set up the properties for error message and text in the custom validator instead of setting them up in the function as I did, would my validation still be considered SERVER SIDE VALIDATION?
From a user perspective, yes... because the page posted back and then came back with the error message.

From a programmer's perspective, yes... because it's all in the codebehind.

You don't want it to be serverside validation... why?
I want it to be all server side validation.;)

That is why I was checking. This control is supposed to be server side vaidation only.
In that case, your question's been answered, yes?
Yup!

Thanks!

No comments:

Post a Comment