Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Required help regarding validating user details using AJAX on a web application  RSS feed

 
Nilesh Sanyal
Ranch Hand
Posts: 33
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to create a registration page where username,email,password will be taken from the user

and if user left any field empty on that form then it will display a message stating that field can not be

left empty. I am trying to use AJAX in this web application.

I have added the jar file "org.json-0.0.1.jar" to the classpath of this web application.

The codes of different files are as follows...

register.html
===========
<html>
<head>
<title>Register Page</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="text/javascript">

</script>
</head>
<body>
<form>
<table align="center">
<tr>
<td bgcolor="lightgreen" colspan="3">Fill up the form</td>
</tr>
<tr>
<td>User Name</td>
<td><input type="text" name="uname" id="name" onkeyup="sendInfo()"/></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email" id="mail" onkeyup="sendInfo()"/></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pass" id="pwd" onkeyup="sendInfo()"/></td>
</tr>
<tr>
<td><input type="submit" value="Register"/></td>
</tr>
</table>

</form>

<div id="errors"></div>
</body>
</html>

RegException.java (This file handles the custom exception)
=====================================


validateServlet.java
=================


After running the web application, if anything is typed(including whitespace character) in any one of the

fields, a the page displays a "null" word. Please help.
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are few things to do here.

You are getting the values in servlet with wrong parameters. You are passing "name" from Javascript, but trying to get "uname" in servlet.
So you are getting a NullPointerException, so it is printing null in browser.

Few suggestions:
Empty field validations should be done on form submit, not onkeyup.
Empty field validation on server side is a good practice, but first level validation should be in client side.
Use JQuery for AJAX, it will make your life very simple.

And try to debug by printing values in Java/debugging if you are using Eclipse/other IDE.
 
Nilesh Sanyal
Ranch Hand
Posts: 33
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for your help, it works fine. Now, I am looking forward to working with jquery to use AJAX.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!