How to avoid the Special characters?

How to avoid the Special characters?

View Answers

March 31, 2009 at 4:07 PM

hi friend,

To avoid special characters we have imposed some validations on the textfields in jsp page.

<html>
<script>
function validate()
{
var chars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
for (var i = 0; i < document.form.text1.value.length; i++) {
if (chars.indexOf(document.form.text1.value.charAt(i)) != -1) {
alert ("Text1 is having special characters.Please remove them.");
return false;
}
}
for (var i = 0; i < document.form.text2.value.length; i++) {
if (chars.indexOf(document.form.text2.value.charAt(i)) != -1) {
alert ("Text2 is having special characters.Please remove them.");
return false;
}
}
for (var i = 0; i < document.form.text3.value.length; i++) {
if (chars.indexOf(document.form.text3.value.charAt(i)) != -1) {
alert ("Text3 is having special characters.Please remove them.");
return false;
}
}
for (var i = 0; i < document.form.text4.value.length; i++) {
if (chars.indexOf(document.form.text4.value.charAt(i)) != -1) {
alert ("Text4 is having special characters.Please remove them.");
return false;
}
}
for (var i = 0; i < document.form.text5.value.length; i++) {
if (chars.indexOf(document.form.text5.value.charAt(i)) != -1) {
alert ("Text5 is having special characters.Please remove them.");
return false;
}

}
if (document.form.text1.value == ""){
alert ( "Please enter text1." );
document.form.text1.focus();
return false;
}
if (document.form.text2.value == ""){
alert ( "Please enter text2." );
document.form.text2.focus();
return false;
}
if (document.form.text3.value == ""){
alert ( "Please enter text3." );
document.form.text3.focus();
return false;
}
if (document.form.text4.value == "")
{
alert ( "Please enter text4." );
document.form.text4.focus();
return false;
}
if (document.form.text5.value == "")
{
alert ( "Please enter text5" );
document.form.text5.focus();
return false;
}
return true;
}
</script>
<body >
<form name="form" action="process.jsp" method="post" onsubmit="return validate()">
<table border="1">
<font size=5>Form</font>
<tr>
<td><b>TextField1</b></td>
<td><input type="text" name="text1" value="" size=50 maxlength=20></td>
</tr>
<tr>
<td><b>TextField2</b></td>
<td><input type="text" name="text2" value="" size=50 maxlength=20></td>
</tr>
<tr>
<td><b>TextField3</b></td>
<td><input type="text" name="text3" value="" size=50 maxlength=20></td>
</tr>
<tr>
<td><b>TextField4</b></td>
<td><input type="text" name="text4" value="" size=50 maxlength=20></td>
</tr>
<tr>
<td><b>TextField5</b></td>
<td><input type="text" name="text5" value="" size=50 maxlength=20></td>
</tr>
<tr>
<td>
<input type="submit" value="Submit"></td><td> <input type="reset" value="Reset">
</td>
</tr>
</table>
</form>
</body>
</html>

March 31, 2009 at 4:08 PM

Now to insert this form into database,use the following code:

<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%
String text1 = request.getParameter("text1");
String text2 = request.getParameter("text2");
String text3 = request.getParameter("text3");
String text4 = request.getParameter("text4");
String text5 = request.getParameter("text5");
String connectionURL = "jdbc:mysql://localhost:3306/test";;
Connection connection = null;
Statement st = null;
Class.forName("com.mysql.jdbc.Driver");
int updateQuery = 0;
try {
connection = DriverManager.getConnection(connectionURL, "root", "root");
String queryString = "INSERT INTO textfield(text1,text2,text3,text4,text5) VALUES ('"+text1+"', '"+text2+"', '"+text3+"', '"+text4+"','"+text5+"')";
st = connection.createStatement();
updateQuery = st.executeUpdate(queryString);
if (updateQuery != 0) {
out.println("Data is inserted successfully.");
}
st.close();
connection.close();
}
catch (Exception ex) {
out.println("Unable to connect to batabase.");
}
%>

Thanks









Related Tutorials/Questions & Answers:
How to avoid the Special characters? - JDBC
How to avoid Java Code in JSP-Files?
Advertisements
How To Protect Special Characters in Query String?
avoid using the whole URL
ModuleNotFoundError: No module named 'avoid_disaster'
ModuleNotFoundError: No module named 'avoid_disaster'
special characters in my HTML
special characters in my HTML
Protect Special Characters.
password validation with special character
regular expression for special characters
How to Avoid Google Penguin Update Penalty
replace special characters in html
How to insert and retrieve Questions with special symbols and images into database in an ONLINE EXAMINATION project?
Escaping Special Characters in a Pattern
ModuleNotFoundError: No module named 'numba-special'
ModuleNotFoundError: No module named 'special-k'
ModuleNotFoundError: No module named 'special_list'
ModuleNotFoundError: No module named 'special-k'
ModuleNotFoundError: No module named 'special_list'
ModuleNotFoundError: No module named 'special_snowflake'
remove special characters iphone
Special Bean Types of WebApplicationContext
avoid duplicate values using struts - Struts
ModuleNotFoundError: No module named 'odoo10-addon-product-special-type'
ModuleNotFoundError: No module named 'odoo10-addon-product-special-type'
What is the special meaning of __sleep and __wakeup?
PHP - Match Special Characters - PHP
special set of tags do in PHP.
avoid java code in jsp - Java Beginners
problems in parsing the xml with the special characters
What?s the special meaning of __sleep and __wakeup?
What does a special set of tags <?= and ?> do in PHP?
javascript regex validate Special character
How to avoid Hotel Telephone Bills
rexexp pattern to avoid certain words and replace space with underscore
How to turn corner of the picture
Avoid Stress using Stress Management
Second Format Example
Minutes Format Example
Day Format Example
Time Format Example
Parsing Date Example
Month Format Example
Hour Format Example
Time Format Example
How many hours a day/a week do you work?
Week Days Format Example
Year Format Example
Want to avoid social media marketing failure? - 6 key tips

Ads