1

I am making a web application using Spring MVC and jsp. I am quite new to this and really struggling to find an approach to validating a username from DB before submitting the form. Here is a demo of the kind of thing am looking for http://www.bitrepository.com/demo/username-checker/

Would appreciate any help given. Thank you

4
  • you can start by following this tutorial mkyong.com/spring-mvc/spring-4-mvc-ajax-hello-world-example Commented Mar 18, 2017 at 0:28
  • This looks really helpful. Correct me if am wrong but the tutorial looks like the validation is done when you press the search button. If so then would it be possible to do the validation before pressing the search button? Commented Mar 18, 2017 at 0:47
  • To be exact, In that tutorial, the ajax call is triggered on onsubmit event (not on onclick event). So you just need to change it to whatever you like. Find the list of possible DOM events here (plain js) and here (jquery version) Commented Mar 18, 2017 at 1:04
  • Thank you very much that has helped me a lot @YohanesGultom Commented Mar 21, 2017 at 11:43

3 Answers 3

1

You can find a lot of answers if you search "Spring MVC form validation" on Google. For example, spring official tutorial: https://spring.io/guides/gs/validating-form-input/

Another example on codejava.net: http://www.codejava.net/frameworks/spring/spring-mvc-form-validation-example-with-bean-validation-api

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

1 Comment

I am already doing form validation when user clicks submit. I wanted it so that the db check is done in the background for username availabilty without the user having to press the submit button.
0

Followed this this tutorial:

http://www.mkyong.com/spring-mvc/spring-4-mvc-ajax-hello-world-example/

Thank you very much @YohanesGultom

Comments

0

you can check username by jquery validation

$("#myform").validate({
  rules: {
    email: {
      required: true,
       remote: {
            url: 'checkUserName',
            data: { userName: $('#username').val()},
            dataFilter: function(data) {
                var json = JSON.parse(data);
                console.log(data);
            }
    }
  },
  messages: {
    email: {
      required: "This field is required",
      remote: "Email address already in use. Please use other email."
    }
  }
});

and in your controller add this method :

@requestMapping("checkUserName")
@responseBody
public boolean  checkUser(@requestParam("userName") String userName){

 return // method return bolean if user exist or non in database.
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.