1

I am building a chat app and I want to ask users to input their username. JQuery front-end code slides a form into view (on ready), stores the data into variables and then loads the chat (when enter key or button is pressed). How do I stop that animation until I validate user input on server-side? I am using node.js for backend. Any ideas?

Thanks in advance!

Front-end jQuery:

var nameChoice, roomChoice; //to store user input

var initName = function() {

		nameChoice = $("#init-name input").val(); //save chosen name in nameChoice
		$("#current-name").text("Username: " + nameChoice); //put chosen name in chat header
		$("#init-name").animate(
			{"left" : "-35%"}, 300,
			function() {
				$(this).addClass("hidden");
				$("#init-room").removeClass("hidden");
				$("#init-room").animate({"left" : "35%"}, 300);
		});  //remove name form and slide in room form in callback
	} //end initName

var initRoom = function() {
		roomChoice = $("#init-room select").val(); //save chosen room in roomChoice
		$("#current-room").text("Room: " + roomChoice); //put chosen room in chat header
		$("#init-room").animate(
			{"left" : "-35%"}, 300,
			function() {
				$(this).addClass("hidden");
				$("#chat-main").removeClass("hidden");
		}); //remove room form and show page in callback
	} //end initRoom

var btnHover = function() {
	$(".btn-form").hover(
		function() {
			$(this).stop().animate(
			{
				backgroundColor : "#FFBD7A"
			}, 300);
		},
		function() {
			$(this).stop().animate(
			{
				backgroundColor : "white"
			}, 300);
		});
}

var init = function() {

	$("#init-name").removeClass("hidden").animate({"left" : "35%"}, 300); //slide in name form
	
	$(document).keydown(function(event) { //submit choice on enter key
		if (event.which === 13) {
			if (!$("#init-name").hasClass("hidden")) { //if user is choosing name
				event.preventDefault();
				initName(); //call initName function
			}

			if (!$("#init-room").hasClass("hidden")) { //if user is choosing room
				event.preventDefault();
				initRoom(); //call initRoom function
			}
		}
	}); //end enter key submission

	$("#init-name .btn-form").click(initName);
	$("#init-room .btn-form").click(initRoom);

	btnHover();	
} //end init
$(document).ready(init);

I'm still learning node, so no back-end code yet...

7
  • please post some code. Commented Dec 23, 2015 at 16:32
  • 1
    But to answer you question in the general sense, when the user hits the enter key, send the data to the back end (AJAX) and have it return a response of either success or failure, then continue with loading the chat if a success comes back, else keep the user on the form. Commented Dec 23, 2015 at 16:34
  • 1
    Alright, now, where is the call that sends the user name to the server? Commented Dec 23, 2015 at 16:52
  • 1
    It can be in another file, but THIS code needs to make a call to the back end asking it to validate the user, you can achieve this with jQuery Ajax. If there is no backend then you don't really have a chat application, you just have a page that looks like chat should exist there; since you need to validate users in some manner. Commented Dec 23, 2015 at 17:23
  • 1
    I recommend looking at these: github.com/IgorAntun/node-chat , github.com/tamaspiros/advanced-chat, and socket.io/get-started/chat you can see how they implemented chat. This is really not about your question, but next steps. Commented Dec 23, 2015 at 17:26

1 Answer 1

2

rough code for this ...

$http.post("/login", {"username":username, "password": password}).then(function(response) {

   console.log("success - do animation here");

}.catch(function(response) {

    console.log("failure a non 2xx HTTP response - handle error here");
});

This code is crude since the http request should prob be in a service, also I have not linted this code, BUT you should get the general IDEA!

APOLOGIES THIS IS ANGULAR, JQUERY WAS ASKED FOR ... HERE GOES ...

$.ajax({
  method: "POST",
  url: "/login",
  data: { username: username, password: password }
})
.then(
  function( data, textStatus, jqXHR ) {
    console.log("success - do animation here");
  }, 
  function( jqXHR, textStatus, errorThrown ) {
    console.log("failure a non 2xx HTTP response - handle error here");
  }
);

Never tried this in Jquery before but the docs suggest this approach. Check the docs at ...

http://api.jquery.com/jquery.ajax/

Thanks

Edit: in case promise based jQuery code not available:

$.ajax({
  method: "POST",
  url: "/login",
  data: { username: username, password: password }
})
// for older versions of jQuery, replace .done and .fail with .success and .error
.done(function( data, textStatus, jqXHR ) {
    console.log("success - do animation here");
})
.fail(function( jqXHR, textStatus, errorThrown ) {
    console.log("failure a non 2xx HTTP response - handle error here");
}); 
Sign up to request clarification or add additional context in comments.

4 Comments

Are you assuming Angular here? along with promises being available? But the general idea applies. Can you also provide a jQuery Ajax example?
I do apologise, I'm so used to answering Angular questions! I've provided the comparative jQuery solution. Hope it helps.
Thank you very much for the answer and examples. I have just one more question: should I just call jQuery functions that I defined (initName, initRoom and so on) instead of console.log("success - do animation here");, or not?
sorry for delay in reply, Christmas and all :) Yeah replace the console.log statement with the code that triggers your animations

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.