How to verify on valid - c#

How can I show a green checkbox image beside textbox on valid usage ?
I'm trying to show this when the user passed to next textbox.
Here is my code for invalid usage
<asp:TextBox ID="TextBoxEMail" runat="server" ValidationGroup="Valid1"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
runat="server" ErrorMessage="EMail is invalid"
ControlToValidate="TextBoxEMail"
ValidationGroup="Valid1" Display="Dynamic" ForeColor="Red"
ValidationExpression="^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*#([0-9a-zA-Z][-\w]*
[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$"></asp:RegularExpressionValidator>

You can do it by using JavaScript (or jQuery).
Here is an example of javascript.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="WebApplication1.Test" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<link type="text/css" rel="stylesheet" href="Content/Site.css" />
<title></title>
<script type="text/javascript">
function checkMailAddress(tb) {
var regEx = RegExp(/^(([^<>()\[\]\\.,;:\s#"]+(\.[^<>()\[\]\\.,;:\s#"]+)*)|(".+"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/);
var img = document.getElementById("imgValidate");
if (regEx.test(tb.value)) {
img.style.display = "block";
}
else
img.style.display = "none";
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtEmail" runat="server" onchange="checkMailAddress(this)"></asp:TextBox>
<asp:Image ID="imgValidate" runat="server" Height="18px" ImageUrl="~/Images/Ok.png" Width="22px" CssClass="validationImage" />
</div>
</form>
</body>
</html>
And in the site.css :
.validationImage
{
display:none;
}
The site.css is in the Content folder in the project. And the image Ok.png is in the Images folder.

Related

Google Recaptcha throwing System.Web.HttpRuntime.WebObjectActivator error

I am trying to use Google Recaptcha on on my web pages. I keep getting an error saying
GoogleReCaptcha.GoogleReCaptcha)(System.Web.HttpRuntime.WebObjectActivator.GetService(typeof(GoogleReCaptcha.GoogleReCaptcha))));
Below is the screen shot:
Below is my aspx page code:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="TryAgain.WebForm1" %>
<%# Register Assembly="GoogleReCaptcha" Namespace="GoogleReCaptcha" TagPrefix="cc1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>
<h1>Google ReCaptcha Form</h1>
<asp:TextBox ID="txt" runat="server"></asp:TextBox>
<cc1:GoogleReCaptcha ID="ctrlGoogleReCaptcha" runat="server" PublicKey="XXXX" PrivateKey="XXX" />
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
<asp:Label ID="lblStatus" runat="server" Text=""></asp:Label>
<asp:Button ID="btn" runat="server" Text="Submit" OnClick="btn_Click" />
</p>
</div>
</form>
</body>
</html>
Below is my aspx.cs file code:
protected void btn_Click(object sender, EventArgs e)
{
if (ctrlGoogleReCaptcha.Validate())
{
//submit form
lblStatus.Text = "Success";
}
else
{
lblStatus.Text = "Captcha Failed!! Please try again!!";
}
}
GoogleRecaptcha that I downloaded has properties like so:
I am struggling with this error for hours. Any help will be highly appreciated.

javascript not working on main content or head content

when i am using javascript code in that format like
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!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>
<script language="javascript" type="text/javascript">
function SetButtonStatus(sender, target) {
var first = document.getElementById('<%=txtfirst.ClientID %>');
var second = document.getElementById('<%=txtText.ClientID %>');
//Condition to check whether user enters text in two textboxes or not
if ((sender.value.length >= 1 && first.value.length >= 1) && (sender.value.length >= 1 && second.value.length >= 1))
document.getElementById(target).disabled = false;
else
document.getElementById(target).disabled = true;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtfirst" runat="server" onkeyup="SetButtonStatus(this,'btnButton')"></asp:TextBox>
<asp:TextBox ID="txtText" runat="server" onkeyup="SetButtonStatus(this,'btnButton')"></asp:TextBox>
<asp:Button ID="btnButton" runat="server" Text="Button" Enabled="false" />
</div>
</form>
</body>
</html>
its runnung perfect and javascript work fine,
but when i am using this javascript code in another page like
<%# Page Title="About Us" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="About.aspx.cs" Inherits="About" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script language="javascript" type="text/javascript">
function SetButtonStatus(sender, target) {
var first = document.getElementById('<%=txtfirst.ClientID %>');
var second = document.getElementById('<%=txtText.ClientID %>');
//Condition to check whether user enters text in two textboxes or not
if ((sender.value.length >= 1 && first.value.length >= 1) && (sender.value.length >= 1 && second.value.length >= 1))
document.getElementById(target).disabled = false;
else
document.getElementById(target).disabled = true;
}
</script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:TextBox ID="txtfirst" runat="server" onkeyup="SetButtonStatus(this,'btnButton')"></asp:TextBox>
<asp:TextBox ID="txtText" runat="server" onkeyup="SetButtonStatus(this,'btnButton')"></asp:TextBox>
<asp:Button ID="btnButton" runat="server" Text="Button" Enabled="false"/>
</asp:Content>
javascript not working and no error issue.
i am using visual studio 2010
asp.net c#.
Try to pass actual ID of button you get after rendering the page. (you can copy from view source)
<asp:TextBox ID="txtfirst" runat="server" onkeyup="SetButtonStatus(this,'ContentPlaceHolder1_btnButton')"></asp:TextBox>
<asp:TextBox ID="txtText" runat="server" onkeyup="SetButtonStatus(this,'ContentPlaceHolder1_btnButton')"></asp:TextBox>
it will work.
Thanks

Get control of main page in showModalDialog?

In my main page i have a label and a button ..on button click i want to open a new popup window using showModalDialog ..In my popup window i have a textbox and a close button ..i want to put something in textbox and click the button . popup need to be close ..Textbox value should show in main page lebel .....
My Main page
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ModalPopupValuePass._Default" %>
<!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>Untitled Page</title>
<script type="text/javascript">
function Abc()
{
debugger;
//window.showModalDialog("FIRE/Popup.aspx",null,"resizable: yes");
var style = 'dialogWidth:350px;dialogHeight:100px;dialogleft:200px;dialogtop:200px;status:no;help:no;';
var respond = window.showModalDialog("FIRE/Popup.aspx", this, '', style);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server"></asp:Label>
<br />
<asp:HiddenField ID="hf1" runat="server"/>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="Abc();"/>
</div>
</form>
</body>
</html>
In cs page of main page
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = hf1.Value;
}
My popup page
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Popup.aspx.cs" Inherits="ModalPopupValuePass.FIRE.Popup" %>
<!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>Untitled Page</title>
<script type="text/javascript">
function Def()
{
debugger;
window.opener.document.getElementById('<%=hf1.ClientID %>').value = document.getElementById('<%=TextBox1.ClientID %>').value;
window.close();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Close" OnClientClick="Def();" />
</div>
</form>
</body>
</html>
But i am not getting the mainpage control in popup ...need help..!!

How to Hide error message of a asp custom validator

I am using asp requiredfieldvalidator. I want the required field validator to fire only when a checkbox on my form is checked.
When the checkbox is fired, the validator works as expected and error message shows up but when the checkbox is unchecked the errormessage that showed up stays on the screen. I have tried different options like validator.resetForm(); disabling the validator, hiding the validator but the error message stays on the screen form. Here is the simplified version of my code:
<script src="jquery-1.4.2.min.js"></script>
<!DOCTYPE html>
<script type="text/javascript">
function enableDisableControls(value) {
var enabledSilentPost = $("#<%=chkEnableSilentPost.ClientID%>").attr("checked");
var validatorControl = $("#<%=valApprovalURL.ClientID%>")[0];
ValidatorEnable(validatorControl, enabledSilentPost);
// If the checkbox is false then assume that the
if (!enabledSilentPost) {
validatorControl.enabled = false;
}
}
$(document).ready(function () {
$("#<%=chkEnableSilentPost.ClientID%>").click(function () {
enableDisableControls();
});
enableDisableControls();
});
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<div>
<asp:CheckBox ID="chkEnableSilentPost" runat="server" Width="250px" Text="Enable" />
<asp:Label ID="lblApprovalURL" runat="server" Text="URL" CssClass="controllabel" meta:resourcekey="lblApprovalURLResource"></asp:Label>
<asp:TextBox ID="txtApprovalURL" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="valApprovalURL" runat="server" ControlToValidate="txtApprovalURL" ErrorMessage="Please enter Valid Text" Text="*"></asp:RequiredFieldValidator>
<actk:ValidatorCalloutExtender ID="extApprovalURL" TargetControlID="valApprovalURL" runat="server" Enabled="True"></actk:ValidatorCalloutExtender>
</div>
</form>
I am was able to get the validators to react by doing the following. The Validator was pretty simpe, but I had to predict the ID of the Extender in order for it to work:
<script type="text/javascript">
$(function () {
$('#<%=chkEnableSilentPost.ClientID %>').click(function () {
$("#<%=valApprovalURL.ClientID %>").toggle(this.checked);
$("#extApprovalURL_popupTable").toggle(this.checked);
});
});
</script>
<form id="form1" runat="server">
<ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" />
<div>
<asp:CheckBox ID="chkEnableSilentPost" runat="server" Width="250px" Text="Enable" />
<asp:Label ID="lblApprovalURL" runat="server" Text="URL" CssClass="controllabel" meta:resourcekey="lblApprovalURLResource"></asp:Label>
<asp:TextBox ID="txtApprovalURL" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="valApprovalURL" runat="server" ControlToValidate="txtApprovalURL" ErrorMessage="Please enter Valid Text" Text="*"></asp:RequiredFieldValidator>
<asp:ValidatorCalloutExtender ID="extApprovalURL" TargetControlID="valApprovalURL" runat="server" Enabled="True"></asp:ValidatorCalloutExtender>
</div>
</form>

Is there a solution to add css template in existing webform which is now having template?

Is there any way to add CSS template for existing webform that has no CSS template added?
My code is listed below, any guidance about how to add CSS template for the form that already exists in the site master will be greatly appreciated:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="Linkchecker.WebForm2"
ValidateRequest="false" EnableViewState="false" EnableViewStateMac="false" EnableSessionState="True"
EnableEventValidation="false" ViewStateEncryptionMode="Never" %>
<!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>
<script type="text/javascript">
function finda() {
var a = document.getElementsByTagName("a");
var b = document.getElementById("TextBox1");
b.value = "";
for (var i = 0; i < a.length; i++) {
a[i] = a.length.value;
if (a[i] == null) {
alert("Their is no links");
}
else {
b.value = b.value + "\r\n\n" + a[i] ;
}
}
// window.open("http://www.fillsim.com");
window.close();
// window.open("WebForm3.aspx?req=" + b.value);
}
</script>
<script>
var howLong = 6000;
t = null;
function closeMe() {
t = setTimeout("self.close()", howLong);
}
</script>
<script type = "text/javascript">
var defaultText = "http://www.example.com";
function waterMarkText(txt, evt) {
if (txt.value.length == 0 && evt.type == "blur") {
txt.style.color = "red";
txt.value = defaultText;
}
if (txt.value == defaultText && evt.type == "focus") {
txt.style.color = "green";
txt.value = "";
}
}
</script>
</head>
<body >
<form id="form1" runat="server">
Enter the URL:<br />
<asp:TextBox ID="urltxt" runat="server" Width="402px" Text="http://www.example.com" ForeColor="Gray" onblur = "waterMarkText(this, event);" onfocus = "waterMarkText(this, event);"></asp:TextBox>
<br />
<br />
<asp:Button ID="btnRender" runat="server" Text="Page Render" OnClick="btnRender_Click" />
<asp:Button ID="btn_submit" runat="server" Text="Submit" OnClientClick="javascript:finda();" />
<asp:Button ID="btn_createlink" runat="server"
Text="Create link" OnClick="btn_createlink_Click" />
<br />
<br />
<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" Height="373px" Width="410px"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" Height="371px" TextMode="MultiLine" Width="409px"></asp:TextBox>
<div class="ab" id="div" runat="server">
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Clear"
Width="71px" />
</div>
</form>
</body>
</html>
You can add in your <head runat="server"> section
<link href="Styles/Site.css" rel="stylesheet" type="text/css" />
<style type="text/css">
.product-bottom-container
{
width: 100%;
overflow: hidden;
height: 10px;
border-top: 1px solid #C41212;
background-color: #ECECEC;
}
</script>
Combinding it
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="Linkchecker.WebForm2"
ValidateRequest="false" EnableViewState="false" EnableViewStateMac="false" EnableSessionState="True"
EnableEventValidation="false" ViewStateEncryptionMode="Never" %>
<!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>
<link href="Styles/Site.css" rel="stylesheet" type="text/css" />
Edit 1
Here is a link how to add master page in an existing page
How to assign a master page to a existing .aspx page?

Categories

Resources