Restrict Special Characters Using JavaScript By Copy,Paste
Restrict Special Characters using copy,paste events in JavaScript.
Please follow these steps:
1. Create a textbox with the name of txtValidation and add event by the name of onpaste to that textbox.
<asp:TextBox ID="txtValidation" runat="server" onpaste="return Validate(this.value);"></asp:TextBox>
2. Create a Function to that event.
<script type="text/javascript">
function Validate(Value) {
var pastedData = window.clipboardData.getData('Text');
var reg = /^[a-zA-Z0-9 ]*$/
if (!reg.test(pastedData)) {
alert('Not Accept Special Characters');
return false;
}
}
</script>
Some of the special characters are hidden when you copy,paste it will not visible. If you want to restrict those special characters in sql database only, not in application.
These Special Characters is not visible when you copy,paste in your application, we have to find in database(tables) only, in that you have to restrict hidden special characters in Sql Database(tables) also.
Please follow these steps:
1. Create a textbox with the name of txtValidation and add event by the name of onpaste to that textbox.
2. Create a button with the name of btnValidate and add event by the name of OnClick to that button.
3. Create a constraint for specific column in your table.
4. Handle that Exception in your code.
Lets See How...
<form id="form1" runat="server">
<div>
<center>
<table>
<tr>
<td>
<asp:Label ID="lblValidation" runat="server" Text="Validation"></asp:Label>
<asp:TextBox ID="txtValidation" runat="server" ></asp:TextBox>
</td>
</tr>
</table>
<asp:Button ID="btnValidate" Text="Validate" runat="server" CausesValidation="false"
OnClick="btnValidate_Click" />
</center>
</div>
</form>
Code behind for btnValidate
protected void btnValidate_Click(object sender, EventArgs e)
{
string connectionString = You Pass your Connection string here;
try
{
int result = InsertData();
if (result > 0)
{
Response.Write("Data Saved Successfully");
}
}
catch (SqlException Sqlex)
{
if (Sqlex.Message.Contains("CHECK constraint"))
{
Response.Write("Special Characters are not Allowed");
}
}
catch (Exception ex)
{
throw ex;
}
}
public int InsertData()
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("INSERT INTO tblValidate (Name) VALUES (@ColumnName)");
cmd.CommandType = CommandType.Text;
cmd.Connection = connection;
cmd.Parameters.AddWithValue("@ColumnName", txtValidation.Text);
connection.Open();
return cmd.ExecuteNonQuery();
}
}
Create a constraint for Specific column.
I am giving my column name as 'Name'.
Your can create a Check Constraint on this column and only allow Number sand Alphabets to be inserted in this column, see below:
Check Constraint to only Allow Numbers & Alphabets
ALTER TABLE Table_Name
ADD CONSTRAINT ck_No_Special_Characters
CHECK (Column_Name NOT LIKE '%[^A-Z0-9]%')
Check Constraint to only Allow Numbers
ALTER TABLE Table_Name
ADD CONSTRAINT ck_Only_Numbers
CHECK (Column_Name NOT LIKE '%[^0-9]%')
Check Constraint to only Allow Alphabets
ALTER TABLE Table_Name
ADD CONSTRAINT ck_Only_Alphabets
CHECK (Column_Name NOT LIKE '%[^A-Z]%')
For Your Reference please check this link for brief description for using SQL Constraints.
http://www.techrepublic.com/blog/how-do-i/how-do-i-reject-alpha-characters-in-a-sql-server-character-column/
Please follow these steps:
1. Create a textbox with the name of txtValidation and add event by the name of onpaste to that textbox.
<asp:TextBox ID="txtValidation" runat="server" onpaste="return Validate(this.value);"></asp:TextBox>
2. Create a Function to that event.
<script type="text/javascript">
function Validate(Value) {
var pastedData = window.clipboardData.getData('Text');
var reg = /^[a-zA-Z0-9 ]*$/
if (!reg.test(pastedData)) {
alert('Not Accept Special Characters');
return false;
}
}
</script>
Some of the special characters are hidden when you copy,paste it will not visible. If you want to restrict those special characters in sql database only, not in application.
These Special Characters is not visible when you copy,paste in your application, we have to find in database(tables) only, in that you have to restrict hidden special characters in Sql Database(tables) also.
Please follow these steps:
1. Create a textbox with the name of txtValidation and add event by the name of onpaste to that textbox.
2. Create a button with the name of btnValidate and add event by the name of OnClick to that button.
3. Create a constraint for specific column in your table.
4. Handle that Exception in your code.
Lets See How...
<form id="form1" runat="server">
<div>
<center>
<table>
<tr>
<td>
<asp:Label ID="lblValidation" runat="server" Text="Validation"></asp:Label>
<asp:TextBox ID="txtValidation" runat="server" ></asp:TextBox>
</td>
</tr>
</table>
<asp:Button ID="btnValidate" Text="Validate" runat="server" CausesValidation="false"
OnClick="btnValidate_Click" />
</center>
</div>
</form>
Code behind for btnValidate
protected void btnValidate_Click(object sender, EventArgs e)
{
string connectionString = You Pass your Connection string here;
try
{
int result = InsertData();
if (result > 0)
{
Response.Write("Data Saved Successfully");
}
}
catch (SqlException Sqlex)
{
if (Sqlex.Message.Contains("CHECK constraint"))
{
Response.Write("Special Characters are not Allowed");
}
}
catch (Exception ex)
{
throw ex;
}
}
public int InsertData()
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("INSERT INTO tblValidate (Name) VALUES (@ColumnName)");
cmd.CommandType = CommandType.Text;
cmd.Connection = connection;
cmd.Parameters.AddWithValue("@ColumnName", txtValidation.Text);
connection.Open();
return cmd.ExecuteNonQuery();
}
}
I am giving my column name as 'Name'.
Your can create a Check Constraint on this column and only allow Number sand Alphabets to be inserted in this column, see below:
Check Constraint to only Allow Numbers & Alphabets
ALTER TABLE Table_Name
ADD CONSTRAINT ck_No_Special_Characters
CHECK (Column_Name NOT LIKE '%[^A-Z0-9]%')
Check Constraint to only Allow Numbers
ALTER TABLE Table_Name
ADD CONSTRAINT ck_Only_Numbers
CHECK (Column_Name NOT LIKE '%[^0-9]%')
Check Constraint to only Allow Alphabets
ALTER TABLE Table_Name
ADD CONSTRAINT ck_Only_Alphabets
CHECK (Column_Name NOT LIKE '%[^A-Z]%')
For Your Reference please check this link for brief description for using SQL Constraints.
http://www.techrepublic.com/blog/how-do-i/how-do-i-reject-alpha-characters-in-a-sql-server-character-column/
Hope It will help You Guys..!!!!
Comments
Post a Comment