I searched SO before posting, but couldn't find an answer that would work for me.
Below is a simple JS script to test a username.
The username length validation work fine BUT the character validation doesn't:
All non-allowed characters (such as $%;[+)) pass whereas they should return an error.
<script type="text/javascript">
$(document).ready(function() {
$("#username").change(function() {
var username = $("#username").val();
var msgbox = $("#status");
var usernameRegex = /^[a-zA-Z0-9][a-zA-Z0-9\._-]$i/
if ((username.length >= 4 && username.length <= 20) &&
(usernameRegex.test(username.value) == false)) {
$.ajax({
type: "POST",
url: "username.php",
data: "username=" + username,
success: function(msg) {
if (msg == 'OK') {
msgbox.html('<img src="ok.png">');
} else {
msgbox.html(msg);
}
}
});
} else {
$("#status").html('<img src="no.png">Try again');
}
});
});
</script>
What am I missing?
usernameRegex.test(username.value) == falsebeusernameRegex.test(username.value) == true?