简体   繁体   中英

To check Username availability in jsp using only java script

Hi I am new to java script im developing a web application where i need to check Username availability on the client side that is when the user inputs his desired Username it should check from MySql database login table and update the status available or not available. I tried searching over the net but i found only Ajax code. I need it in Java script so some one help me in this regard.

This is my Username JSP for your reference..

<tr>
    <td> UserName* :</td>
    <td>
        <input type="text" name="Username" id="username"/><br/>
       </td>
</tr>

What you want to do can't be done out of the box and could be very insecure.
Please have a look to this link: can I fetch record from mysql using jquery.
Another link: tutorials jquery and mysql.

you want security, you want do in javascript, both thing cant be happen.

its better to ajax calling for availability of username
<input type="text" name="txtUsername" id="userName"/>@gmail.com<br/>

OR

sends the all usernames which are occupied by other users on the page. then use it, pass them to javascript and store them an array. and then checks availability( not recommended because its not safe ).

ajax is 100 times secure then storing in javascript

Clarifying what Govind is saying here. To check the availability of usernames client side would require having all the usernames client side.

Also, even if you do implement an ajax solution, you need to be mindful that you are still exposing your usernames to anyone willing to write a loop.

Easiest somewhat secure solution is to rate limit the responses for username validation, and use jQuery form validation for your test.

Don't fear the ajax. It is simply a GET request to a url you expose server side, which returns the string true/false. The form validation plugin takes care of making the request and doing what you want on pass/fail.

$( "#myform" ).validate({
  rules: {
    email: {
      required: true,
      email: true,
      remote: "check-email.php"
    }
  }
});

jQuery makes the ajax validation easy.

Related question with code. (php, but you'll get it.)

you can do it with jquery in your jsp something like this:

<tr>
<td>Choose your UserName* :</td>
<td><script type="text/javascript" src="jquery.js"></script>
<input type="text" name="txtUsername" id="username">@gmail.com
<div id="status"></div>
<script type="text/javascript" src="js/check_user.js"></script>
<span id="errorMissingUserName" style="display:none;"><font color="red">*Please provide your username.</font></span>
<span id="errorUserNameInvalid" style="display:none;"><font color="red">*Please provide a valid username.Username can contain only alphabets numbers and periods</font></span>
<span class="status"></span>
</tr>

go through this [link][ check user name avialability using jsp and mysql... There is a little problem with the servlet jdbc i think if you resolve it will be k try it....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM