sudha
How to avoid the Special characters?
2 Answer(s)      6 years and 4 months ago
Posted in : JDBC
Advertisement
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