Javascript验证文本框 [英] Javascript Validation of textbox

查看:82
本文介绍了Javascript验证文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友



我有以下表格,我对文本框验证感到困惑请帮帮我



Hi friends

I have following form and I am confused for validation of text box please help me

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Ajax Demo</title>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>

    <script type="text/javascript">

        function ValidateName()
        {
            if (document.getElementById("txt_name").value.length == 0)
            {
                document.getElementById("txt_name").focus();
                alert("Empty string detected.");
                return false;
            }
            return true;
        $(document).ready(function () {

            $("#<%= btn_Insert.ClientID %>").click(function () {

                 var email = document.getElementById('txt_email').value;
                 var name = document.getElementById('txt_name').value;
                 document.getElementById('<%= btn_Insert.ClientID %>').disabled = true;

                //$("#btn_Insert").fadeOut();
                //alert("{'Name':'" + name + "', 'Email':'" + email + "'}");
                $.ajax({

                    type: 'POST',

                    contentType: "application/x-www-form-urlencoded; charset=utf-8",

                    data: "Name=" + name + "&Email=" + email,

                    async: true,

                    success: function (response) {
                        $('#txt_name').val('');
                        $('#txt_email').val('');

                        alert(response);

                        document.getElementById('<%= btn_Insert.ClientID %>').disabled = false;

                    },

                    error: function () {
                        console.log('there is some error');
                    }

                });
                return false;
            });
         });

 </script>
</head>
<body>
    <table align="center">
        <tr>
            <td>
               <form id="form1" runat="server">
                <div>
                    <p>
                        Name : <asp:TextBox ID="txt_name" runat="server" ClientIDMode="Static" Onblur="javascript:ValidateName();"></asp:TextBox>
                                <span id="error_name" runat="server"></span>
                    </p>

                    <p>
                        E-mail : <asp:TextBox ID="txt_email" runat="server" ClientIDMode="Static"></asp:TextBox>
                                <span id="error_email" runat="server"></span>
                    </p>

                    <p>
                        <asp:Button ID="btn_Insert" runat="server" Text="INSERT" OnClick="btn_Insert_Click"/>
                    </p>
                </div>
                </form>
            </td>
        </tr>
    </table>

</body>
</html>







我想验证文本框,如果第一个是空的,那么它不允许在第二个上移动,当我填充两个然后它允许点击buton




I want to validate text box if first is empty then it not allow to move on second one and when I fill both then and then it allow to click buton

推荐答案

document )。ready( function (){
(document).ready(function () {


#<%= btn_Insert。 ClientID%>)。click( function (){

var email = document .getElementById(' txt_email 。)值;
var name = document .getElementById(' txt_name')。value;
document .getElementById(' <% = btn_Insert.ClientID%>')。disabled = true ;

//
("#<%= btn_Insert.ClientID %>").click(function () { var email = document.getElementById('txt_email').value; var name = document.getElementById('txt_name').value; document.getElementById('<%= btn_Insert.ClientID %>').disabled = true; //


(#btn_Insert ).fadeOut();
// alert({'Name':' + name +','Email':'+ email +'});
("#btn_Insert").fadeOut(); //alert("{'Name':'" + name + "', 'Email':'" + email + "'}");


这篇关于Javascript验证文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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