来自业务逻辑层的服务器端验证功能 [英] Server side validation function from Business Logic Layer

查看:117
本文介绍了来自业务逻辑层的服务器端验证功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,亲爱的开发人员,我有2个文本框,我要检查字符串长度(超过2个字符)和要求.问题是我不能将此服务器端验证放在页面后面的代码中,而是应该调用一个应在项目的BLL文件中创建的函数.我知道如何在后面的代码中对此进行验证,但是在另一个文件中创建此类函数并调用它给我带来了麻烦.任何帮助将不胜感激.谢谢.
N.B:
这是我为2个文本框创建的2个customValidator后面的代码中的验证函数:

 受保护的  void  LNCustomValidator_ServerValidate(对象源,ServerValidateEventArgs参数)
        {


            如果(LNameText.Text.Length <   2 )
            {
                args.IsValid =  false ;
                lblLnameValid.Visible =  true ;

            }
            其他
            {
                args.IsValid =  true ;
                lblLnameValid.Visible =  false ;
            }
        } 


 受保护的 无效 FNCustomValidator_ServerValidate(对象源,ServerValidateEventArgs参数)
       {
           如果(FNameText.Text.Length ==  1 )
           {
               args.IsValid =  false ;
               lblFnameValid.Visible =  true ;
               EmpDataView.Visible =  false ;

           }
           其他
           {
               args.IsValid =  true ;
               lblFnameValid.Visible =  false ;
           }
       } 


我还对输入的字符进行了验证,即仅允许使用字母字符.为此,我对两个文本框的Textchanged事件执行了以下操作:

 受保护的 无效 LNameText_TextChanged(对象发​​件人,EventArgs e)
       {
           字符串 oldText = 字符串 .Empty;
           如果(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 = " ;

           }
           其他
           {

               LNameText.Text = oldText;
               LNameText.BorderColor = System.Drawing.Color.Red;
               LNameText.ForeColor = System.Drawing.Color.Black;
               lblLnameValid.Visible =  true ;
             LNCustomValidator.Text = " ;

           }
       } 


 受保护的 无效 FNameText_TextChanged(对象发​​件人,EventArgs e)
        {
            字符串 oldText = 字符串 .Empty;
            如果(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 = " ;

            }
            其他
            {

                FNameText.Text = oldText;
                FNameText.BorderColor = System.Drawing.Color.Red;
                FNameText.ForeColor = System.Drawing.Color.Black;
                lblFnameValid.Visible =  true ;
                FNCustomValidator.Text = " ;

            }
        } 


有什么建议吗? (从一个函数执行此验证,我应该在一个称为业务逻辑层"的文件中调用该函数).

要满足这两个要求,无需使用自定义验证"控件..
-只需在.aspx
<script type="text/javascript">标记中添加以下功能 -在
中写入标签onblur="return isValidLengh();" & onkeypress="return isAlphabet(event);" LnameText文本框...

 函数 isAlphabet(evt){
     var  charCode =(evt.which)吗? evt.which:event.keyCode;
    如果(((charCode>  96 && charCode<  64 && charCode< 返回 返回 ;
} 


 函数 isValidLength(){
     var  tb = 文档 .getElementById(' <%= LNameText.ClientID%>');
    如果(tb!= ){
        如果(tb.value.length< 3){
                alert(" );
                tb.focus();
                返回 ;
             }
             返回 ;
    }
} 


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)

解决方案

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;
    }
}


这篇关于来自业务逻辑层的服务器端验证功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆