Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
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:
C#
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;
            }
        }

C#
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:
C#
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";

           }
       }

C#
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
Comments
[no name] 11-Sep-12 10:57am    
Okay, and? Add those functions to your BLL DLL then and call them. What exactly is the problem?
Predator008 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.
[no name] 11-Sep-12 11:04am    
You don't handle the events in another location (DLL). You call your functions from the event handler.
Predator008 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())?
[no name] 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.

1 solution

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...
JavaScript
function isAlphabet(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode;
    if ((charCode > 96 && charCode < 123) || (charCode > 64 && charCode < 91))
        return true;
    return false;
}

JavaScript
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;
    }
}
 
Share this answer
 
v3
Comments
_Amy 12-Sep-12 5:04am    
This is client side validation. Please read the question carefully.
Predator008 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900