简体   繁体   English

使用jQuery验证文本框

[英]Validate text box using jquery

I have several asp text box inside my aspx page. 我的aspx页面中有几个asp文本框。 And there is no enough space to show Requred field validator messages. 并且没有足够的空间来显示Requred字段验证器消息。 Is there any solution to this? 有什么解决办法吗? using jquery or something else? 使用jQuery或其他?

You can simply highlight the text-box with Red border to mark that their is an issue with the text-box data. 您可以简单地突出显示带有Red边框的文本框,以标记它们是文本框数据的问题。

You can use CSS like this: 您可以像这样使用CSS:

input.error  
{ 
   background: Yellow; border: 1px solid red; 
}

On error, just add the class to the input and also input tooltip 发生错误时,只需将类添加到input然后再输入工具提示

$("input").addClass("error").attr('title', 'An error occurred!');

Try below code : 试试下面的代码:

$(":text").each(function(){
    if($(this).val()==""){
    $(this).css("background-color","red");
    alert($(this).attr("id") & " validate error");  
    return
    }

});

You can use both javascript or jQuery, by validate data and then turn back your message in to the input if it is not validated. 您可以通过验证数据来使用javascript或jQuery,然后将消息转回输入(如果未验证)。 ex: 例如:

if($('#idOfFeild').val()==''){
    $('#idOfFeild').val('Please input your data');
}

You can add style too to warn your users! 您也可以添加样式来警告用户!

You can read this article : http://www.codeproject.com/Articles/43772/Generic-Code-of-Validating-Fields-with-Jquery and them try this code : 您可以阅读这篇文章: http : //www.codeproject.com/Articles/43772/Generic-Code-of-Validating-Fields-with-Jquery ,他们可以尝试以下代码:

<script type="text/javascript">
    <script src="JavaScript/jquery.js" type="text/javascript"></script>
   function callOnload()
   {
      $("input[isvalidate=true]").each(
                              function(n)
                              {
                                 $('#' +this.id).addClass('mandatory');
                                 this.onkeyup=function()
                                 {
                                    if(this.value==='')
                                    {
                                       $('#' +this.id).removeClass('normal');
                                       $('#' +this.id).addClass('mandatory');
                                    }
                                    else
                                    {
                                       $('#' +this.id).removeClass('mandatory');
                                       $('#' +this.id).addClass('normal');
                                    }
                                 }
                              }
                        );

         $("#btnSubmit").click(
                              function()
                              {
                                  $("input[isvalidate=true]").each(
                                  function(n)
                                  {
                                     if(this.value==='')
                                     {
                                        alert($('#' +this.id).attr('errprMsg'));
                                     }
                                  }
                                 );
                                return false;
                              }
                           );
   }
   $(document).ready(callOnload);
</script>

Yes obviously, you have a very simple solution. 是的,显然,您有一个非常简单的解决方案。

you don't have to use any jquery stuff. 您不必使用任何jquery东西。 you just have to use asp:validationSummary . 您只需要使用asp:validationSummary

I'm posting here solution. 我在这里发布解决方案。 with this you can show all error messages in simple popup by using standard asp validation controls . 这样,您可以使用标准的ASP validation controls在简单的弹出窗口中显示所有错误消息。 It won't consume any space on your page . 它不会占用您page上的任何空间。

Have a look on code below: 看下面的代码:

<asp:ValidationSummary ID="validation1" runat="server" ShowMessageBox="true"  ShowSummary="false" DisplayMode="SingleParagraph" ValidationGroup="abc" />

<asp:TextBox ID="txtAge" runat="server" ></asp:TextBox>

<asp:RequiredFieldValidator ID="req" runat="server" ControlToValidate="txtAge" Display="None" ValidationGroup="abc" ErrorMessage="Field Required"></asp:RequiredFieldValidator>

 <asp:Button ID="btn" Text="click"  runat="server" ValidationGroup="abc" />

Here i have set ShowMessageBox="true" in Validation Summary , which will show all error messages in popup. 在这里,我在ShowMessageBox="true" Validation Summary ShowMessageBox="true"中设置了ShowMessageBox="true" ,它将在弹出窗口中显示所有错误消息。

Set your validators Display="none" , so that the error messages display only in the popup. 将验证器设置为Display="none" ,以便仅在弹出窗口中显示错误消息。

the error message will look like below image. 错误消息如下图所示。 You don't have to worry about to manage space of page. 您不必担心管理页面空间。

在此处输入图片说明

It is perfect , easy and simple solution for your problem. 它是您的问题的完美,简便的解决方案。

Let me know, if it works for you. 请让我知道这对你有没有用。

Alternativly to alert() you can use your own validator and color text or textbox. 另外,对于alert(),您可以使用自己的验证器,并为文本或文本框添加颜色。 I have got small example on my page and you can adapt it. 我的页面上有一个小示例,您可以对其进行调整。 Add css class: http://kjinit.blogspot.com/2013/03/jquery-dynamic-adding-css-class.html and remove css class. 添加CSS类: http : //kjinit.blogspot.com/2013/03/jquery-dynamic-adding-css-class.html并删除CSS类。

Of course first you need to validate it like this: if('#yourInputId').val(''); 当然,首先您需要像这样验证它:if('#yourInputId')。val(''); Just color your text adding or removing css class when it is correct or not. 正确或不正确时,只需为添加或删除css类的文本着色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM