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?
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");
    }
}
                        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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With