Below is a description of the problem, the "check() function" code, and the "register.html" file:

Problem description:

I haven't been able to write correct javascript (and maybe my check function has a problem that I haven't caught) to make an alert pop up telling the user that they must choose another username, because they entered one that is already taken (ie: it already exists in the database). I intended to have that alert appear "onkeyup" when entering a username in register.html, thinking that it would appear as soon as I finish entering a username that I already have in the database. The problem is that when I enter a username in "register.html" that is already in the database, the alert never pops up. How can I fix that?

check() function code from "application.py":

@app.route("/check", methods=["GET"])
def check():
    """Return true if username available, else false, in JSON format"""
    username = request.form.get("username")
    if len("username") >= 1:
        rows=db.execute("SELECT username FROM users")
        for row in rows:
            if username == row["username"]:
                response = False
            elif username != row["username"] in rows[len(rows) - 1]:
                response = True
    return jsonify("response")

Here is the "register.html" file with the javascript in lower portion:

{% extends "layout.html" %}

{% block title %}
{% endblock %}

{% block main %}
   <form action="/register" method="post"> 
        <div class="form-group">
            <input autocomplete="off" id="username" onkeyup = 
"function();" autofocus class="form-control" name="username" 
placeholder="Username" type="text">
        <div class="form-group">
            <input class="form-control" name="password" 
placeholder="Password" type="password">
        <div class="form-group">
            <input class="form-control" name="confirmation" 
placeholder="Confirm Password" type="password">
        <button class="btn btn-primary" type="submit">Register</button>


        document.querySelector("input").onkeyup = function() {
            $.get('/check?username=' +username.value, function() {
                else if(false)
                    alert("That username is already taken. Please 
choose another username.");

{% endblock %}

Thank you.

  • I haven't looked at your js, but your check function should only return true or false and nothing else. It shouldn't render an apology. As its comment says: """Return true if username available, else false, in JSON format""" – curiouskiwi Jul 15 '19 at 21:44
  • @curiouskiwi, so I've edited my question, posting the check() function and register.html that I currently have. I still have the same problem as before, which is basically that when I enter a username, on the "register" page, that is already in the database(that username is already in the database), the alert never pops up, and I can't figure out (so far, at least) how to fix my code. Thank you. – John Abdelmalek Jul 18 '19 at 2:49
  • load in your browser <your finance url>/check?username=testuser which will show you what your check function is returning. Is it True or False? Or something else? – curiouskiwi Jul 18 '19 at 8:03

You must log in to answer this question.

Browse other questions tagged .