RSS

Category Archives: ASP.Net Validation

disable validation of hidden div or row


in this tutorial  try to resolve the issue of Validation when the dropdownlist hidden which show only when radio button click .the problem i face when radio button click or not click which give error message you must select value of dropdownlist .so resolve the problem with java script function.you apply this procedure on checkbox or anyone.

<table cellpadding=”0″ cellspacing=”0″>
<tr>
<td>
<input id=”CbBranch” runat=”server” type=”radio” value=”Branch” name=”ctl00$ContentPlaceHolder1$rbl2″
onclick=”validateFormApp()” />
<label for=”ctl00_ContentPlaceHolder1_rbl2_4″>
Branch</label>
</td>
<td style=”padding: 3px 0 0 4px; vertical-align: middle;”>
<div id=”trBranch” style=”display: none;”>
*
<asp:DropDownList ID=”ddlBranch” runat=”server” CssClass=”input”>
<asp:ListItem Value=”-1″>Select Branch</asp:ListItem>
<asp:ListItem>Chesapeake Branch</asp:ListItem>
<asp:ListItem>Largo Branch</asp:ListItem>
<asp:ListItem>Cherokee Branch</asp:ListItem>
<asp:ListItem>Reston Branch</asp:ListItem>
<asp:ListItem>McPherson Square Branch</asp:ListItem>
<asp:ListItem>Potomac Avenue </asp:ListItem>
<asp:ListItem>Columbia Heights Branch</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator

ID=”rfvbranch” runat=”server” ControlToValidate=”ddlBranch”
Display=”None” ErrorMessage=”Select Branch ” InitialValue=”-1″ ValidationGroup=”grpSur”></asp:RequiredFieldValidator>
</div>
</td>
</tr>
</table>

Javascript Function for hide and show and also check the validation

<script language=”JavaScript” type=”text/JavaScript”>
function validateFormApp() {

var tr_branch = document.getElementById(‘trBranch’);

var rfv_branch = document.getElementById(‘<%=rfvbranch.ClientID %>’);

if (document.getElementById(“<%=CbBranch.clientid %>”).checked == true) {

document.getElementById(‘trBranch’).style.display = ”

ValidatorEnable(rfv_branch, true);

}
else {

document.getElementById(‘trBranch’).style.display = ‘none’

ValidatorEnable(rfv_branch, false);
}
}

//onclick=”if(this.checked==true){document.getElementById(‘trBranch’).style.display=”;} else{document.getElementById(‘trBranch’).style.display=’none’ EnableValidation=”false”;}”
</script>

Onload disable the validation of Hidden div or row of the dropdownlisth

<script language=”JavaScript” type=”text/JavaScript”>
//call after page loaded
window.onload = validateFormApp;
</script>


 
Leave a comment

Posted by on March 22, 2011 in ASP.Net Validation

 

html validation with popup


 <script language=”JavaScript” type=”text/JavaScript”>
<!–
        function MM_findObj(n, d) { //v4.01
            //n=”ctl00_ContentPlaceHolder1_” + n;
            var p, i, x; if (!d) d = document; if ((p = n.indexOf(“?”)) > 0 && parent.frames.length) {
                d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
            }
            if (!(x = d[n]) && d.all) x = d.all[n]; for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
            for (i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n, d.layers[i].document);
            if (!x && d.getElementById) x = d.getElementById(n); return x;
        }

        function MM_validateForm() { //v4.0
            var i, p, q, nm, test, num, min, max, errors = ”, args = MM_validateForm.arguments;
            for (i = 0; i < (args.length – 2); i += 3) {
                test = args[i + 2]; val = MM_findObj(args[i]);
                if (val) {
                    nm = val.name; ; if ((val = val.value) != “”) {
                        if (test.indexOf(‘isEmail’) != -1) {
                            p = val.indexOf(‘@’);
                            if (p < 1 || p == (val.length – 1)) errors += ‘- ‘ + nm + ‘ must contain an e-mail address.\n’;
                        } else if (test != ‘R’) {
                            num = parseFloat(val);
                            if (isNaN(val)) errors += ‘- ‘ + nm + ‘ must contain a number.\n’;
                            if (test.indexOf(‘inRange’) != -1) {
                                p = test.indexOf(‘:’);
                                min = test.substring(8, p); max = test.substring(p + 1);
                                if (num < min || max < num) errors += ‘- ‘ + nm + ‘ must contain a number between ‘ + min + ‘ and ‘ + max + ‘.\n’;
                            }
                        }
                    } else if (test.charAt(0) == ‘R’) errors += ‘- ‘ + nm + ‘ is required.\n’;
                }
            } if (errors) alert(‘The following error(s) occurred:\n’ + errors);
            document.MM_returnValue = (errors == ”);
        }
        //–>
        function ShowTextFocus(obj, size, ids) {
            if (obj.value.length == size) {
                document.getElementById(ids).focus();
            }
        }
    </script>

html code

 

 <h1>
        Contact Us</h1>
    <form id=”form1″ runat=”server”>
    <label>
        First Name: *</label>
    <input name=”Member_Name” type=”text” value=”” />
    <br />
    <label>
        Last Name: *</label>
    <input name=”Last_Name” type=”text” value=”” />
    <br />
    <label>
        Account Number: *</label>
    <input type=”text” name=”Account_Number” />
    <br />
    <label>
        Email Address: *</label>
    <input type=”text” name=”Email” />
    <br />
    <label>
        Day Time Phone: *</label>
    <input type=”text” name=”Day_Phone_1_1″ id=”Day_Phone_1_1″ size=”3″ maxlength=”3″
        onkeyup=”ShowTextFocus(this,3,’Day_Phone_1_2′);” style=”width: 50px” />
    –
    <input type=”text” name=”Day_Phone_1_2″ id=”Day_Phone_1_2″ size=”3″ maxlength=”3″
        onkeyup=”ShowTextFocus(this,3,’Day_Phone_1_3′);” style=”width: 50px” />
    –
    <input type=”text” name=”Day_Phone_1_3″ id=”Day_Phone_1_3″ size=”4″ maxlength=”4″
        onkeyup=”ShowTextFocus(this,4,’Evening_Phone_1_1′);” style=”width: 50px” />
    <br />
    <label>
        Evening Phone:
    </label>
    <input type=”text” name=”Evening_Phone_1_1″ id=”Evening_Phone_1_1″ size=”3″ maxlength=”3″
        onkeyup=”ShowTextFocus(this,3,’Evening_Phone_1_2′);” style=”width: 50px” />
    –
    <input type=”text” name=”Evening_Phone_1_2″ id=”Evening_Phone_1_2″ size=”3″ maxlength=”3″
        onkeyup=”ShowTextFocus(this,3,’Evening_Phone_1_3′);” style=”width: 50px” />
    –
    <input type=”text” name=”Evening_Phone_1_3″ id=”Evening_Phone_1_3″ size=”4″ maxlength=”4″
        style=”width: 50px” />
    <br />
    <label>
        Best time to call:
    </label>
    <input type=”text” name=”Call_Time” />
    <br />
    <label>
        What is your question in regards to?</label><br />
    <select name=”Question”>
        <option>Member Services</option>
        <option>Loans</option>
        <option>Mortgages</option>
        <option>Driver’s Choice</option>
        <option>Human Resources</option>
        <option>Business Services</option>
    </select>
    <%–<br />
              <label>Question / Comments:</label>
              <br />
              <textarea name=”Comments” cols=”50″></textarea>–%>
    <br />
    <label>
        Additional Remarks:</label>
    <textarea id=”txtAdtRmrks” name=”txtAdtRmrks” rows=”7″ cols=”55″></textarea>
    <br />
    <br />
    <div style=”text-align: center”>
        <asp:Button ID=”btnSubmit” runat=”server” Text=”submit” OnClientClick=”MM_validateForm(‘Member_Name’,”,’R’,’Last_Name’,”,’R’,’Account_Number ‘,”,’R’,’Email’,”,’RisEmail’,’Day_Phone_1_1′,”,’RisNum’,’Day_Phone_1_2′,”,’RisNum’,’Day_Phone_1_3′,”,’RisNum’,’Evening_Phone_1_1′,”,’NisNum’,’Evening_Phone_1_2′,”,’NisNum’,’Evening_Phone_1_3′,”,’NisNum’);return document.MM_returnValue” />
        <input id=”Reset1″ type=”reset” value=”Reset” />
    </div>
    </form>
    <br />
    <br />

 

Access these control value to code behind(vb.net)

Import namespaces

 


Imports System.Net
Imports System.Net.Mail
Imports System.Configuration.ConfigurationManager

 

 Dim strMemberName As String = HttpUtility.HtmlEncode(Trim(Request.Form(“Member_Name”)))
            Dim strlName As String = HttpUtility.HtmlEncode(Trim(Request.Form(“Last_Name”)))
            Dim strMemberNumber As String = HttpUtility.HtmlEncode(Trim(Request.Form(“Account_Number”)))
            Dim strEmail As String = HttpUtility.HtmlEncode(Trim(Request.Form(“Email”)))

            Dim strDayPhoneNo1_1 As String = HttpUtility.HtmlEncode(Trim(Request.Form(“Day_Phone_1_1”)))
            Dim strDayPhoneNo1_2 As String = HttpUtility.HtmlEncode(Trim(Request.Form(“Day_Phone_1_2”)))
            Dim strDayPhoneNo1_3 As String = HttpUtility.HtmlEncode(Trim(Request.Form(“Day_Phone_1_3”)))

            Dim strEveningPhoneNo1_1 As String = HttpUtility.HtmlEncode(Trim(Request.Form(“Evening_Phone_1_1”)))
            Dim strEveningPhoneNo1_2 As String = HttpUtility.HtmlEncode(Trim(Request.Form(“Evening_Phone_1_2”)))
            Dim strEveningPhoneNo1_3 As String = HttpUtility.HtmlEncode(Trim(Request.Form(“Evening_Phone_1_3”)))

            Dim strCallTime As String = HttpUtility.HtmlEncode(Trim(Request.Form(“Call_Time”)))

            Dim strQuestion As String = HttpUtility.HtmlEncode(Trim(Request.Form(“Question”)))

            ‘Dim strComments As String = HttpUtility.HtmlEncode(Trim(Request.Form(“Comments”)))

            Dim txtAdtRmrks As String = HttpUtility.HtmlEncode(Trim(Request.Form(“txtAdtRmrks”)))


    <strong>* </strong>Fields are required.
 

 
Leave a comment

Posted by on March 22, 2011 in ASP.Net Validation

 

Textbox to allow integer only


You should look at using a RegularExpressionValidator. Validator may ASPX page, which you control with the strengthening of property ControlToValidate

Code;

  <asp:RegularExpressionValidator id="RegularExpressionValidator1" 
                     ControlToValidate="TextBox1"
                     ValidationExpression="^\d+$"
                     Display="Static"
                     ErrorMessage="value must be an integer"
                     runat="server"/>


n your button you would set the CausesValidation property to true.



<asp:Button ID="SomeButton"
                    Text="Submit"
                    CausesValidation="True"
                    runat="server"/>
 
Leave a comment

Posted by on September 21, 2010 in ASP.Net Validation