简体   繁体   中英

Check if username already exists in SQLite using JavaScript

I am new to JavaScript and am making a restaurant app which consists of a registration form. I want to check if the username already exists and if it does exist it should throw an alert to the user on button click.

JavaScript code:

<script type="text/javascript">
var db;
var shortname="R_loginDB";
var version = "1.0";
var displayName = "R_loginDB";
var maxSize = 90000;

function onBodyLoad() {
    db = openDatabase(shortname,version,displayName,maxSize);
    db.transaction(function(tx){
        tx.executeSql('CREATE TABLE IF NOT EXISTS R(UserName TEXT NOT NULL PRIMARY KEY,Password TEXT NOT NULL,ConPassword TEXT NOT NULL)');
    });
}

function ListDBValues() {
    if (!window.openDatabase) {
        alert('Databases are not supported in this browser.');
        return;
    }

    $('#output').html('');

    db.transaction(function(transaction) {
        transaction.executeSql('SELECT * FROM R;', [], function(transaction, result) {
            if (result != null && result.rows != null) {
                for (var i = 0; i < result.rows.length; i++) {
                    var row = result.rows.item(i);
                    $('#output').append('<br>' +  ' ' + row.UserName+ ' ' + row.Password + ' '+ row.ConPassword);
                }
            }
        });
    });

    return;
}

ListDBValues();

function AddValues() {
    var flag = false;
    if ($("#pass").val() != $("#conpass").val()) {
        alert("Passwords do not match.");
        window.location.href = "r_register.html";
    } else if (flag == false) {
        try {
            db.transaction(function(transaction) {
                transaction.executeSql('INSERT INTO R(UserName, Password, ConPassword) VALUES (?,?,?)',[$('#uname').val(),$('#pass').val(),$('#conpass').val()]);
            });
            flag = true;
            window.location.href = "login.html";
        } catch(err) {
            alert("Username " + $("#uname").val() + " is already taken!! Please chose another one");
            window.location.href = "r_register.html";
        }
    }
}

AddValues();
</script>

HTML code:

<body onload="onBodyLoad()">
<div id="main">
    <img id="bg" src="file:///android_asset/www/images/bigtable_1_blur.jpg"/>

    <input id="uname" type="text" placeholder="    Username"></input>
    <input id="pass" type="password" placeholder="    Password"></input>
    <input id="conpass" type="password" placeholder="    Confirm Password"></input>
    <a href="login.html"><p id="login"><b>Already have an account?<i><u>Login</u></i></b></p></a>

    <!-- <a href="second.html"> -->
    <input id="btn" type="button" value="Register" onclick="AddValues()"></input><br/>

    <!-- </a> --> 
    <input id="btn2" type="button" value="show"  onclick="ListDBValues()"/></input>

    <span id="output"></span>
</div>
</body>

WebSQL API is depricated. It is unlikely to ever be supported on platforms that don't currently support it, and it may be removed from platforms that do.

Try using localStorage instead. Check out more at Cordova storage documentation

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