Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET validation
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)
Posted 11-Sep-12 4:47am
Comments
Wes Aday at 11-Sep-12 10:57am
   
Okay, and? Add those functions to your BLL DLL then and call them. What exactly is the problem?
Member 9372390 at 11-Sep-12 11:01am
   
Hey, the problem is that those functions now exist in my main page code behind, I want to place them in a file named BLL in which there is a Class file. I want to put them in this Class file and call them from there. The logic how to do this is not clear to me, how to handel the ServerValidate and TextChanged events from another location.
Wes Aday at 11-Sep-12 11:04am
   
You don't handle the events in another location (DLL). You call your functions from the event handler.
Member 9372390 at 11-Sep-12 11:09am
   
suppose I create a function called LNameValidate() in BLL file, now I want to call it form my main page in whcich my customvalidator resides, as you said I should go to the events and on Server Validate cell put the name of that function (BLL.LNameValidate())?
Wes Aday at 11-Sep-12 11:16am
   
Unless you you know of some other way of doing it, yes. It's exactly the same as calling any other function in any other DLL. I am not understanding what your confusion is.
Member 9372390 at 11-Sep-12 11:20am
   
could you plz translate that to a concrete example? like how I should declare my function in BLL file? what parameters to pass?
Wes Aday at 11-Sep-12 11:26am
   
You can't write a simple function? You are kidding me right? bool LNameValidate(string name)
{
// do your validation
if(valid)
return true;
else
return false;
}
Member 9372390 at 11-Sep-12 11:32am
   
:D yeah I can. I've wrote something like that to check characters entred
 
public bool validateCharacters(string input, string msg) {
 
const string regExpr = @"^[a-zA-Z]+$"; //regular expression for valid string
 
// match input with regular expression
 
System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(input, regExpr);
 
if (m.Success) { msg = null;
 
return true; } else {
 
msg= "characters"; return false; }
 
}and like this to check requiered field:
 
public bool RequiredLname(string txt, string msg) {
 
if (string.IsNullOrEmpty(txt)) {
 
msg = "Last name required"; return false;
 
} else { msg = null;
 
return true; } }
 
But I would like to do is use the IsValid in my main page to check the validation using my customvalidators.
Wes Aday at 11-Sep-12 11:38am
   
Okay.... so? YourClassName validator = new YourClassName(); bool valid = validator.IsValid(input, msg);
Member 9372390 at 11-Sep-12 11:54am
   
I've done like you said before but it didn't work, maybe I am missing something or I misplaced the logic behind this.
thnks anyway Wes
Jyothikarthik_N at 12-Sep-12 2:25am
   
"It didn't work"
 
When you executed/ran your application, what exactly happened? Was it an error, exception or just something that went against your expectations?
Member 9372390 at 12-Sep-12 8:56am
   
I got it work, i still have just some minor problems in the execution of the ifs. here are the ifs i ve put in my logic:
protected void SearchButton_Click(object sender, EventArgs e)
{
 
BLL.EmpLNameValidate bl = new BLL.EmpLNameValidate();
 

if (!bl.RequiredLname(LNameText.Text))
{
Label1.Text = "Last Name is required!";
lblLnameValid.Visible = true;
EmpDataView.Visible = false;
}
else if(!bl.LenghtValidate(LNameText.Text))
{
Label1.Text = "Please enter at least 2 characters!";
lblLnameValid.Visible = true;
EmpDataView.Visible = false;
}
else if (!CharactersValidate(LNameText.Text))
{
Label1.Text = "Only alphabets are allowed!";
lblLnameValid.Visible = true;
EmpDataView.Visible = false;
}
else if (FNameText.Text.Length > 0 & !bl.LenghtValidate(FNameText.Text))
{
Label2.Text = "Please enter at least 2 characters!";
lblFnameValid.Visible = true;
EmpDataView.Visible = false;
}
else if (FNameText.Text.Length > 0 & !CharactersValidate(FNameText.Text))
{
Label2.Text = "Only alphabets are allowed!";
lblFnameValid.Visible = true;
EmpDataView.Visible = false;
}
else
{
lblLnameValid.Visible = false;
lblFnameValid.Visible = false;
Label1.Text = null;
Label2.Text = null;
BindData();
 
if (EmpDataView.Rows.Count == 0)
{
 
NewUserLabel.Visible = true;
NewUserButton.Visible = true;
 
}
else
{
EmpDataView.Visible = true;
NewUserLabel.Visible = false;
NewUserButton.Visible = false;
}
 
}
}
 
where my BLL file (Class) looks like this:
 
namespace Phonebook.BLL
{
// TODO Validate that the user have entred characters for Last Name field
public class EmpLNameValidate
{
 
// to validate last name is required
public bool RequiredLname(string txt)
{
if (!string.IsNullOrEmpty(txt))
{
return true;
}
else
{

return false;
}
}
 
// to validate 2 characters lenght
public bool LenghtValidate(string txt)
{
if (txt.Length < 2)
{
return false;
 
}
else
{
return true;
}
 
}
}
}
 
i run it it's working but still some minor "inpurities" in the ifs.
Member 9372390 at 11-Sep-12 11:16am
   
to be clear here is what I want:
 
I have a search button and a GridViewcontrol, whaen a user clicks the button the validation sould validate that the last name has been filled in, and if so it should contain at least 2 characters, then if the user chooses to include first name, he should also enter 2 characters, but it's not a required field:
 
protected void SearchButton_Click(object sender, EventArgs e) {
 
//if validation passesBindData();
 
if (EmpDataView.Rows.Count == 0) {
 
NewUserLabel.Visible = true;
 
NewUserButton.Visible = true; }
 
else {
 
EmpDataView.Visible = true;
 
NewUserLabel.Visible = false;
 
NewUserButton.Visible = false; }
 
// else show error messages

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You have two requirements...Follow this steps two achieve both requirments.
 
To solve your both requirements no need to use Custom Validation control..
- just add following function in <script type="text/javascript"> tag in .aspx
- Write tags onblur="return isValidLengh();" & onkeypress="return isAlphabet(event);" in
LnameText Textbox...
function isAlphabet(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode;
    if ((charCode > 96 && charCode < 123) || (charCode > 64 && charCode < 91))
        return true;
    return false;
}
function isValidLength(){
    var tb = document.getElementById('<%= LNameText.ClientID %>');
    if(tb != null){
        if(tb.value.length<3){
                alert("Atlest two characters required");
                tb.focus();
                return false;
             }
             return true;
    }
}
  Permalink  
v3
Comments
_Amy at 12-Sep-12 5:04am
   
This is client side validation. Please read the question carefully.
Member 9372390 at 12-Sep-12 10:14am
   
indeed it is client side validation, thanks anyway NothingToLoose and _Amy

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 14 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100