code in aspx:
<asp:HiddenField ID="ContentType" runat="server" />
code behind:
public partial class _Default : System.Web.UI.Page
{
public void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["SalesDBConnectionString"].ToString();
con.Open();
SqlDataReader read;
SqlCommand cmdtest = new SqlCommand("SELECT * FROM Details_Instructor", con);
cmdtest.CommandType = CommandType.Text;
read = cmdtest.ExecuteReader();
ArrayList myArrList = new ArrayList();
try
{
while (read.Read())
{
myArrList.Add(read["InstructorPhone"].ToString());
}
for (int i = 0; i <= 10; i++)
{
var testString = myArrList[i].ToString();
TextBox1.Text += (testString + '\n');
ClientScript.RegisterClientScriptBlock(typeof(Page), "formValidation", "isValidPhoneNumber('" + testString + "', true, true)", true);
string testHiddenField = Request.Form["ContentType"];
TextBox3.Text += testHiddenField;
}
}
catch (SqlException ex)
{
TextBox2.Text = ex.ToString();
}
finally
{
read.Close();
con.Close();
TextBox3.Text += "End";
}
}
public void Button1_Click(object sender, EventArgs e)
{
}
}
last part of code from the javascript file where I try to pass a value (newPhone):
var myTextBox = document.getElementById("ContentType");
myTextBox = newPhone;
Am I doing something wrong with how I assign a value to the hidden field?
string testHiddenField = ContentType.Value;
and JS should be
var myTextBox = document.getElementById("ContentType");
myTextBox.value = newPhone;
ar myTextBox = document.getElementById("<%=ContentType.ClientID%>");
myTextBox.value = newPhone;
var myTextBox = document.getElementById("<%=ContentType.ClientID%>");
myTextBox.value = newPhone;
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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>DBTest</title>
<style type="text/css">
#TextArea1
{
height: 350px;
width: 240px;
}
#TextArea2
{
height: 350px;
width: 240px;
margin-left: 245px;
margin-top: 0px;
}
</style>
<script type="text/javascript">
function isValidPhoneNumber (elem, blnPrompt, blnAllowEmpty)
{
if (typeof(blnPrompt) == 'undefined' ) blnPrompt = true;
if (typeof(blnAllowEmpty) == 'undefined' ) blnAllowEmpty = true;
var str = elem;
var phoneError = /[^extnsio\d\(\)\-\.\s\+\:\;\,]/gi;
if(!blnAllowEmpty && !isNotEmpty(elem, blnPrompt)){
return false;
}
if(blnAllowEmpty && str == ""){
return true;
}
if (substr_count(str, '(') != substr_count(str, ')'))
{
if(blnPrompt){
alert("Please check your phone number");
}
elem.select();
elem.focus();
return false;
}
function StringBuffer()
{
this.buffer = [];
}
StringBuffer.prototype.append = function append(string)
{
this.buffer.push(string);
return this;
};
StringBuffer.prototype.toString = function toString()
{
return this.buffer.join("");
};
var plusAppend = new StringBuffer();
var cleanPhone = str;
// cleans up the phone number and takes out all characters not a number
var cleanPhoneNonNumeral = cleanPhone.replace(/[^\d,]/gi, "");
// phone number length
var phoneDigits = cleanPhoneNonNumeral.length;
var newPhone = "";
var valid = false;
switch(phoneDigits)
{
case 10:
{
var areaCodePhone = /\(?[0-9]{3}\)?(.|-| ){0,5}[0-9]{3}(.|-| )?[0-9]{4}/;
if(str.match(areaCodePhone))
{
valid = true;
newPhone = plusAppend + "(" + cleanPhoneNonNumeral.substring(0,3) + ") " + cleanPhoneNonNumeral.substring(3,6) + "-" + cleanPhoneNonNumeral.substring(6,10);
break;
}
}
default:
{
alert("Please double-check the phone number you've entered");
return false;
}
}
if(valid)
{
elem = newPhone;
var myTextBox = document.getElementById("ContentType");
myTextBox.value = newPhone;
}
else
{
elem = str;
}
return true;
}
function substr_count( searchString, subSearchString, offset, length )
{
var pos = 0, count = 0;
searchString += '';
subSearchString += '';
if(isNaN(offset)) offset = 0;
if(isNaN(length)) length = 0;
offset--;
while( (offset = searchString.indexOf(subSearchString, offset+1)) != -1 )
{
if(length > 0 && (offset+subSearchString.length) > length)
{
return false;
}
else
{
count++;
}
}
return count;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:HiddenField ID="ContentType" runat="server" />
<div>
</div>
<p style="margin-top: 2px">
<asp:TextBox ID="TextBox1"
runat="server" Height="400px" TextMode="MultiLine" Width="250px"
ReadOnly="True"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" Height="400px" TextMode="MultiLine"
Width="250px" ReadOnly="True"></asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server" Height="400px" Width="250px"
ReadOnly="True" TextMode="MultiLine"></asp:TextBox>
</p>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
</asp:UpdatePanel>
<p style="margin-top: 2px">
<asp:Button ID="btnStart" runat="server" onclick="Button1_Click" Text="Start" />
</p>
</form>
</body>
</html>
ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestForm.aspx.cs" Inherits="TestForm" %>
<!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 id="Head1" runat="server">
<title>DBTest</title>
<style type="text/css">
#TextArea1
{
height: 350px;
width: 240px;
}
#TextArea2
{
height: 350px;
width: 240px;
margin-left: 245px;
margin-top: 0px;
}
</style>
<script type="text/javascript">
function isValidPhoneNumber (elem, blnPrompt, blnAllowEmpty)
{
if (typeof(blnPrompt) == 'undefined' ) blnPrompt = true;
if (typeof(blnAllowEmpty) == 'undefined' ) blnAllowEmpty = true;
var str = elem;
var phoneError = /[^extnsio\d\(\)\-\.\s\+\:\;\,]/gi;
if(!blnAllowEmpty && !isNotEmpty(elem, blnPrompt)){
return false;
}
if(blnAllowEmpty && str == ""){
return true;
}
if (substr_count(str, '(') != substr_count(str, ')'))
{
if(blnPrompt){
alert("Please check your phone number");
}
elem.select();
elem.focus();
return false;
}
function StringBuffer()
{
this.buffer = [];
}
StringBuffer.prototype.append = function append(string)
{
this.buffer.push(string);
return this;
};
StringBuffer.prototype.toString = function toString()
{
return this.buffer.join("");
};
var plusAppend = new StringBuffer();
var cleanPhone = str;
// cleans up the phone number and takes out all characters not a number
var cleanPhoneNonNumeral = cleanPhone.replace(/[^\d,]/gi, "");
// phone number length
var phoneDigits = cleanPhoneNonNumeral.length;
var newPhone = "";
var valid = false;
switch(phoneDigits)
{
case 10:
{
var areaCodePhone = /\(?[0-9]{3}\)?(.|-| ){0,5}[0-9]{3}(.|-| )?[0-9]{4}/;
if(str.match(areaCodePhone))
{
valid = true;
newPhone = plusAppend + "(" + cleanPhoneNonNumeral.substring(0,3) + ") " + cleanPhoneNonNumeral.substring(3,6) + "-" + cleanPhoneNonNumeral.substring(6,10);
break;
}
}
default:
{
alert("Please double-check the phone number you've entered");
return false;
}
}
if(valid)
{
elem = newPhone;
var myTextBox = document.getElementById("ContentType");
myTextBox.value = newPhone;
}
else
{
elem = str;
}
return true;
}
function substr_count( searchString, subSearchString, offset, length )
{
var pos = 0, count = 0;
searchString += '';
subSearchString += '';
if(isNaN(offset)) offset = 0;
if(isNaN(length)) length = 0;
offset--;
while( (offset = searchString.indexOf(subSearchString, offset+1)) != -1 )
{
if(length > 0 && (offset+subSearchString.length) > length)
{
return false;
}
else
{
count++;
}
}
return count;
}
</script>
</head>
<body onload="ValidateForm();">
<form id="form1" runat="server">
<asp:HiddenField ID="ContentType" runat="server" />
<div>
</div>
<p style="margin-top: 2px">
<asp:TextBox ID="TextBox1"
runat="server" Height="400px" TextMode="MultiLine" Width="250px"
ReadOnly="True"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" Height="400px" TextMode="MultiLine"
Width="250px" ReadOnly="True"></asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server" Height="400px" Width="250px"
ReadOnly="True" TextMode="MultiLine"></asp:TextBox>
</p>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
</asp:UpdatePanel>
<p style="margin-top: 2px">
<asp:Button ID="btnStart" runat="server" onclick="Button1_Click" Text="Start" />
</p>
</form>
</body>
</html>
CODEBEHIND
public partial class TestForm : System.Web.UI.Page
{
public void Page_Load(object sender, EventArgs e)
{
try
{
var js = "function ValidateForm(){\n";
for (int i = 0; i <= 10; i++)
{
var testString = "(123) 456-7890";
TextBox1.Text += (testString + '\n');
js += "isValidPhoneNumber('" + testString + "', true, true)\n";
string testHiddenField = ContentType.Value;
TextBox3.Text += testHiddenField;
}
js += "}";
ClientScript.RegisterClientScriptBlock(this.GetType(), "formValidation", js , true);
}
catch (Exception ex)
{
TextBox2.Text = ex.ToString();
}
finally
{
TextBox3.Text += "End";
}
}
public void Button1_Click(object sender, EventArgs e)
{
}
}
Code Behind:
public partial class _Default : System.Web.UI.Page
{
public void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["SalesDBConnectionString"].ToString();
con.Open();
SqlDataReader read;
SqlCommand cmdtest = new SqlCommand("SELECT * FROM Details_Instructor", con);
cmdtest.CommandType = CommandType.Text;
read = cmdtest.ExecuteReader();
ArrayList myArrList = new ArrayList();
try
{
while (read.Read())
{
myArrList.Add(read["InstructorPhone"].ToString());
}
var js = "function ValidateForm(){\n";
for (int i = 0; i <= 10; i++)
{
var testString = myArrList[i].ToString();
TextBox1.Text += (testString + '\n');
js += "isValidPhoneNumber('" + testString + "', true, true)\n";
var testHiddenField = ContentType.Value;
TextBox3.Text += testHiddenField;
}
js += "}";
ClientScript.RegisterClientScriptBlock(this.GetType(), "formValidation", js, true);
}
catch (Exception ex)
{
TextBox2.Text = ex.ToString();
}
finally
{
TextBox3.Text += "End";
}
}
public void Button1_Click(object sender, EventArgs e)
{
}
}
var myTextBox = document.getElementById("C
myTextBox.value = newPhone;