1

i am using jsp for registration form. I use servlet for verification of existing username. how can i write client side validation code in javasript. My registration form is

<form action="Registration" method="post" name="registration" >
<table>
   <tr>
    <td>Name</td>
    <td>:<input type="text" name="name" value="" placeholder="Name"    
class="input_Name"></td>
</tr>
<tr>
    <td>Place</td>
    <td>:<input type="text" name="place" value="" placeholder="Place"    
class="input_Place"></td>
</tr>
<tr>
    <td>Gender</td>
    <td>:<input type="radio" value="male" name="gender">Male
    <input type="radio" value="female" name="gender">Female</td>
</tr>
<tr>
    <td>UserName</td>
    <td>:<input type="text" name="username" value="" placeholder="UserName" 
  class="input_UserName"></td>
</tr>
<tr>
    <td>Password</td>
    <td>:<input type="password" name="password" value="" placeholder="Password" 
 class="input_Password"></td>
</tr>
<tr>
    <td>Confirm sword</td>
    <td>:<input type="password" name="password" value="" placeholder="re-
 Password" class="input_re-Password"></td>
</tr>

<tr>
    <td></td>
    <td><input type="submit" value="submit" ></td>
</tr>
 </table>
 </form>

thank you..

0

1 Answer 1

0

one way to deal with that is this code:

<td>
    <input type="submit" value="submit" onclick='validate()'>
</td>

<script type="text/javascript">
    function validate() {
        var val = registration.password.value;
        if (val == null || val.trim() == "") {
            alert('Please enter password.');
            registration.password.focus();
            return false; // cancel submission
        } else {
            document.registration.submit(); // allow submit
        }
    }
</script>

if the password field has a value the submit will be executed, otherwise it will be canceled and the focus is set to the password field.

Sign up to request clarification or add additional context in comments.

2 Comments

But My form have action property. when am click submitt button it goes to the servlet for verifying if the username is existing or not. before goes to the server i want check the required field validation, confirm validation...
thats the reason for the onClick='validate()' method for your submit button. that will be executed before the request is started to the server.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.