Hi Dear developers, I am having 2 textboxes which I want to check for string lenght (more than 2 characters) and for requirement. the problem is that I can't put this server-side validation in code behind of my page, instead I should call a function I should create in BLL file in my project. I know how to validate this on code behind, but creating such a function in another file and call it is giving me a hard time. Any help would be grateful. Thanks.
N.B:
here is my validation functions in code behind for 2 customValidators i've created for the 2 textboxes:
protected void LNCustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
{
if (LNameText.Text.Length < 2)
{
args.IsValid = false;
lblLnameValid.Visible = true;
}
else
{
args.IsValid = true;
lblLnameValid.Visible = false;
}
}
protected void FNCustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
{
if (FNameText.Text.Length == 1)
{
args.IsValid = false;
lblFnameValid.Visible = true;
EmpDataView.Visible = false;
}
else
{
args.IsValid = true;
lblFnameValid.Visible = false;
}
}
I have also made a validation on characters entered, i.e. only alphabetical ones are allowed. For this I have done this on Textchanged events of the 2 textboxes like this:
protected void LNameText_TextChanged(object sender, EventArgs e)
{
string oldText = string.Empty;
if (LNameText.Text.All(chr => char.IsLetter(chr)))
{
oldText = LNameText.Text;
LNameText.Text = oldText;
LNameText.BorderColor = System.Drawing.Color.Empty;
LNameText.ForeColor = System.Drawing.Color.Black;
LNCustomValidator.Text = "Please enter at least 2 characters";
}
else
{
LNameText.Text = oldText;
LNameText.BorderColor = System.Drawing.Color.Red;
LNameText.ForeColor = System.Drawing.Color.Black;
lblLnameValid.Visible = true;
LNCustomValidator.Text = "Only alphabetical characters";
}
}
protected void FNameText_TextChanged(object sender, EventArgs e)
{
string oldText = string.Empty;
if (FNameText.Text.All(chr => char.IsLetter(chr)))
{
oldText = FNameText.Text;
FNameText.Text = oldText;
FNameText.BorderColor = System.Drawing.Color.Empty;
FNameText.ForeColor = System.Drawing.Color.Black;
FNCustomValidator.Text = "First Name search must contain at least 2 characters";
}
else
{
FNameText.Text = oldText;
FNameText.BorderColor = System.Drawing.Color.Red;
FNameText.ForeColor = System.Drawing.Color.Black;
lblFnameValid.Visible = true;
FNCustomValidator.Text = "Only alphabetical characters";
}
}
Any suggestion? (do this validation from a function I should call in a file called Business Logic Layer)