Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

subdomain name validation in javascript/jquery

I have a signup form where users kan enter their subdomain of choice when creating an account.

http://_________.ourapp.com

I want them to be able to enter valid characters on the ____________________ part above only. I'm using a text field for that.

Is there a function or some sort of pattern that exists for such situations? Spaces should be filtered, I guess many or all special characters (except numbers, dash and letters) as well?

like image 360
Jorre Avatar asked Jul 04 '10 22:07

Jorre


2 Answers

you can use Regular Expressions to achieve what you need.

Try something like this:

<input id="username" type="text" onblur="validSubdomain()" />


function validSubdomain() {
    var re = /[^a-zA-Z0-9\-]/;
    var val = document.getElementById("username").value;
    if(re.test(val)) {
        alert("invalid");
    }
}
like image 80
Marko Avatar answered Oct 28 '22 10:10

Marko


Try if(subdomainName.match(/^[a-z0-9][a-z0-9\-]*[a-z0-9]$/))) {...what to do if valid here...} else {...invalid handling here...} - I reckon that ought to work.

like image 45
Will A Avatar answered Oct 28 '22 10:10

Will A