dmjio
10/11/2013 - 4:16 PM

validateUser.coffee

re = /[^\s@]+@[^\s@]+\.[^\s@]+/
validEmail = false
$(document).ready ->
        btn = $('#submit')
        email = $('#email')
        pass = $('#password')
        vtext = $('#validation')
        btn.attr('disabled','disabled')
        changeHandler = (p, e) ->
                res = re.test e.val()
                len = p.val().length
                if not res # Email not valid
                        vtext.text("Invalid email")
                        vtext.css("color","red")
                        btn.attr('disabled','disabled')
                        email.addClass('has-error')
                        return
                if len < 7 # passworld length not valid
                        vtext.css("color", "red")
                        vtext.text("Password too short")
                        btn.attr('disabled','disabled')
                        return
                $.ajax
                  url: "/validateUser/"
                  type: "POST"
                  success: (e) ->
                        if e is false 
                                btn.removeAttr('disabled')
                                vtext.text("Success")
                                vtext.css("color","green")
                        else # email taken? Forgot password?
                                vtext.text("");
                                vtext.css("color", "black")
                                vtext.append("In use, <a href='/forgot'>Forgot your password?</a>")
                                btn.attr('disabled','disabled')
                        return
                  data: JSON.stringify({ "email" : email.val() })
                  contentType: 'application/json; charset=utf-8',
                  dataType: 'json',
                  async: false,
                  error: (e) ->
                          console.log("There was a server error")
                          return
                return
        email.keyup ->
                changeHandler(pass, email)
                return
        pass.keyup ->
                changeHandler(pass, email)
                return
        email.change ->
                changeHandler(pass, email)
                return
        pass.change ->
                changeHandler(pass, email)
                return
        return