Subdomain name validation in javascript / jquery

I have a registration form in which users can enter their subdomain of choice when creating an account.

http://_________.ourapp.com

I want them to be able to enter valid characters only on the ____________________ page. I use a text box for this.

Is there any function or pattern that exists for such situations? Spaces should be filtered out, I think that many or all special characters (except numbers, dashes and letters) also?

+3
source share
4 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");
    }
}
+3
source

if(subdomainName.match(/^[a-z0-9][a-z0-9\-]*[a-z0-9]$/))) {...what to do if valid here...} else {...invalid handling here...} - , .

0

Javascript 1.2 . .

" " , - : , , "" ( , ).

entry.Match(/^[a-zA-Z0-9\-]+$/)

, , . , , JavaScript -. , javascript (, , ).

0

, : " ?"

:

  • :
    • 26 ( )
    • (0-9)
    • / (-)
  • , ;
  • 1 63 ;
  • 255

, . ourapp.com ben.ourapp.com, ben, ourapp com - . , :

ben.franklin.ourapp.com
i.have.a.clever.vho.st

In these cases, you can allow the user's child domain more than 63 characters (63 * the number of periods in the child domain with a maximum size of 244 ( .ourapp.com11 characters).

See this Wikipedia article for more information on valid host names.

Edit: If you want to support internationalized domain names , things get a little more complicated, manageable.

-3
source

Source: https://habr.com/ru/post/1753085/


All Articles