bansee bansee - 1 year ago 144
Javascript Question

other than statement in javascript

basically i have a username field in my website which i am trying to filter that user should insert characters only allowed in a username field.

any option is much appreciated but i'd prefer something like

String allowedchars = "a-zA-Z0-9~`!@#()";
if(username.value.contains other than allowedchars){

} else {


of course there is no kind of statement for other than but there could be some kind of function that could get the job done.

Answer Source

You could use a regexp to check if the whole input string contains but the specified allowed chars:

if (username.value.match(/^[a-zA-Z0-9~`!@#\(\)]+$/)) {
    // username is valid

(My regex also assumes that the input must at least contains 1 char, otherwise you should replace + by *)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download