我们可以使用JAVASCRIPT将文本框的值插入到sql数据库中

时间:2014-04-21 13:03:02

标签: javascript

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <table>
        <tr>
            <td>
                <fieldset>
                    <legend><b>Personal Details</b></legend>
                    <table>
                        <tr>
                            <td>
                                <b>*FirstName</b>
                            </td>
                            <td>
                                <asp:TextBox ID="txtfname" runat="server"></asp:TextBox>
                            </td>
                            <td>
                                <b>*LastName</b>
                            </td>
                            <td>
                                <asp:TextBox ID="txtLname" runat="server"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <b>*SurName</b>
                            </td>
                            <td colspan="3">
                                <asp:TextBox ID="Txtsurname" runat="server"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <b>Phone</b>
                            </td>
                            <td>
                                <asp:TextBox ID="Txtphone" runat="server"></asp:TextBox>
                            </td>
                            <td>
                                <b>Extension</b>
                            </td>
                            <td>
                                <asp:TextBox ID="Txtextension" runat="server"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <b>Mobile</b>
                            </td>
                            <td colspan="3">
                                <asp:TextBox ID="txtmob" runat="server"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <b>*Sex</b>
                            </td>
                            <td>
                                <asp:RadioButton ID="rdbmale" runat="server" />
                                <b>Male</b>
                                <asp:RadioButton ID="rdfemale" runat="server" />
                                <b>Female</b>
                            </td>
                            <td>
                                <b>Maritial Status</b>
                            </td>
                            <td>
                                <asp:RadioButton ID="rdmarry" runat="server" />
                                <b>Married</b>
                                <asp:RadioButton ID="Rdsingle" runat="server" />
                                <b>Single</b>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <b>*Email Id</b>
                            </td>
                            <td colspan="2">
                                <asp:TextBox ID="txtEmailId" runat="server" Width="100%"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <b>Address</b>
                            </td>
                            <td colspan="3">
                                <asp:TextBox ID="txtaddress" runat="server" TextMode="MultiLine" Width="100%"></asp:TextBox>
                            </td>
                        </tr>
                    </table>
                </fieldset>
            </td>
        </tr>
        <tr>
            <td>
                <fieldset>
                    <legend><b>Education Details</b></legend>
                    <table>
                        <tr>
                            <td>
                                <b>*Qualification </b>
                            </td>
                            <td>
                                <asp:TextBox ID="txtqual" runat="server"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <b>*College</b>
                            </td>
                            <td>
                                <asp:TextBox ID="txtclg" runat="server"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <b>*University</b>
                            </td>
                            <td>
                                <asp:TextBox ID="Txtuniv" runat="server"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <b>*Year Of Passing</b>
                            </td>
                            <td>
                                <asp:DropDownList ID="ddlyop" runat="server">
                                    <asp:ListItem Text="select" Value="-1"></asp:ListItem>
                                    <asp:ListItem Text="1990" Value="0"></asp:ListItem>
                                    <asp:ListItem Text="1991" Value="1"></asp:ListItem>
                                    <asp:ListItem Text="1992" Value="2"></asp:ListItem>
                                    <asp:ListItem Text="1993" Value="3"></asp:ListItem>
                                    <asp:ListItem Text="1994" Value="4"></asp:ListItem>
                                    <asp:ListItem Text="1995" Value="5"></asp:ListItem>
                                    <asp:ListItem Text="1996" Value="6"></asp:ListItem>
                                    <asp:ListItem Text="1997" Value="7"></asp:ListItem>
                                    <asp:ListItem Text="1998" Value="8"></asp:ListItem>
                                    <asp:ListItem Text="1999" Value="9"></asp:ListItem>
                                    <asp:ListItem Text="2000" Value="10"></asp:ListItem>
                                    <asp:ListItem Text="2001" Value="11"></asp:ListItem>
                                    <asp:ListItem Text="2002" Value="12"></asp:ListItem>
                                    <asp:ListItem Text="2003" Value="13"></asp:ListItem>
                                    <asp:ListItem Text="2004" Value="14"></asp:ListItem>
                                    <asp:ListItem Text="2005" Value="15"></asp:ListItem>
                                    <asp:ListItem Text="2006" Value="16"></asp:ListItem>
                                    <asp:ListItem Text="2007" Value="17"></asp:ListItem>
                                    <asp:ListItem Text="2008" Value="18"></asp:ListItem>
                                    <asp:ListItem Text="2009" Value="19"></asp:ListItem>
                                    <asp:ListItem Text="2010" Value="20"></asp:ListItem>
                                    <asp:ListItem Text="2011" Value="21"></asp:ListItem>
                                    <asp:ListItem Text="2012" Value="22"></asp:ListItem>
                                    <asp:ListItem Text="2013" Value="23"></asp:ListItem>
                                    <asp:ListItem Text="2014" Value="24"></asp:ListItem>
                                    <asp:ListItem Text="2015" Value="25"></asp:ListItem>
                                    <asp:ListItem Text="2016" Value="26"></asp:ListItem>
                                    <asp:ListItem Text="2017" Value="27"></asp:ListItem>
                                    <asp:ListItem Text="2018" Value="28"></asp:ListItem>
                                    <asp:ListItem Text="2019" Value="29"></asp:ListItem>
                                    <asp:ListItem Text="2020" Value="30"></asp:ListItem>
                                </asp:DropDownList>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <b>Address</b>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2">
                                <asp:TextBox ID="txtadd1" runat="server" Width="100%" TextMode="MultiLine"></asp:TextBox>
                            </td>
                        </tr>
                    </table>
                </fieldset>
            </td>
        </tr>
        <tr>
            <td colspan="4" align="center">
                <asp:Button ID="btnsubmit" runat="server" Text="Submit" />
            </td>
        </tr>
    </table>
    </form>
</body>
</html>

in this page i need to make a clent side validations for all mandatory fields 

像文本框,单选按钮,使用java脚本的下拉列表......因为我对此非常新颖所以

请使用javascript帮助我完成各种类型的验证

我对.cs文件中的验证有一点了解,但在处理javascript验证时我没有pror知识

3 个答案:

答案 0 :(得分:1)

试试这个javascript函数: -

function validateForm(){
 var mandatoryFields = document.getElementById('txtfname').value=="" || document.getElementById('txtLname').value=="";
 //and other mandatory textboxes with id
 if(madatoryFields){
   alert("Please fill all mandatory fields.")
   return;
 }
}

<asp:Button ID="btnsubmit" runat="server" Text="Submit" OnClick="validateForm"/>

答案 1 :(得分:0)

试试这个..

<script type="text/javascript" language="javascript">
        function Validate() {
            if(document.getElementById('txtfname').value=="")
            {
               alert('txtfname empty');
            }

             if(document.getElementById('rdmarry').checked==false)
            {
               alert('rdmarry not selected');
            }
             var ddlyop= document.getElementById('ddlyop');
            if( ddlyop.options[ddlyop.selectedIndex].value=="")
            {
                  alert('ddlyop not selected');
            }
       </script>

您可以验证与此相同的所需控件。

您可以在按钮的onclientclick事件上调用此函数..

<asp:Button ID="btnSave" runat="server" Text="Save"  OnClientClick="Validate();return false;"/> 

答案 2 :(得分:0)

首先你可以在按钮的“Onclick”上附加一个javascript方法(ID =“btnsubmit”),如:

<asp:Button ID="btnsubmit" runat="server" Text="Submit" onclick="Validate()"/>

并在脚本标记中定义此javascript方法,如:

<script type="text/javascript" language="javascript">
        function Validate() {
            if(document.getElementById('txtfname').value==""||
               document.getElementById('txtlname').value=="" ||                 
               document.getElementById('Txtsurname').value==""||
               document.getElementById('Txtphone').value=="" ||
               document.getElementById('Txtextension').value=="" ||
               document.getElementById('txtaddress').value=="" ||
               document.getElementById('ddlyop').value=="") //add fields which you wants to validate here
            {
               alert("Mandatory information missing");//your specific message
            }
       </script>