Current location: Hot Scripts Forums » General Web Coding » JavaScript » How to validate a subdomain?


How to validate a subdomain?

Reply
  #1 (permalink)  
Old 08-17-09, 12:11 PM
Mythvn Mythvn is offline
Wannabe Coder
 
Join Date: Jan 2006
Posts: 111
Thanks: 2
Thanked 0 Times in 0 Posts
How to validate a subdomain?

A valid subdomain may contain letters, digits and many minus signs inside ( - ) but may not starts or ends with a single minus sign. Eg : a-b, cd--12... are valid subdomains but -ab, cd12- are not. Can anyone tell me how to validate a subdomain with Javascript please? Thanks
Reply With Quote
  #2 (permalink)  
Old 08-17-09, 03:45 PM
=OTS=G-Man =OTS=G-Man is offline
Newbie Coder
 
Join Date: Jun 2009
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
Regular expressions.

this is the expression you could use

^[^-][a-zA-Z-]+

this is untested but should work.

HTML Code:
<script language="JavaScript1.2">
function checksubdomain()
{
    var validsub=/^[^-][0-9a-zA-Z-]+/ //regular expression defining a string not starting with -'s but can contain alpha, numbers and -'s
    if (document.myform.myinput.value.search(validsub)==-1) //if match failed
        alert("Subdomains can not start with -'s")
}
</script>

<form name="myform">
<input type="text" name="myinput" size=15>
<input type="button" onClick="checksubdomain()" value="check">

</form>
Hope this helps
Reply With Quote
  #3 (permalink)  
Old 08-17-09, 03:47 PM
=OTS=G-Man =OTS=G-Man is offline
Newbie Coder
 
Join Date: Jun 2009
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
opps change that expression to ^[^-][0-9a-zA-Z-]+[^-]$ to block names ending with a - also
Reply With Quote
  #4 (permalink)  
Old 08-17-09, 09:20 PM
Mythvn Mythvn is offline
Wannabe Coder
 
Join Date: Jan 2006
Posts: 111
Thanks: 2
Thanked 0 Times in 0 Posts
Wow that's really good. It works perfectly. Thank you very much for your help
Reply With Quote
  #5 (permalink)  
Old 08-18-09, 11:07 AM
Mythvn Mythvn is offline
Wannabe Coder
 
Join Date: Jan 2006
Posts: 111
Thanks: 2
Thanked 0 Times in 0 Posts
Oops, your regex forgets domains that are shorter than 3 characters, eg : a, cd, e0.... Can you change it please?
Reply With Quote
  #6 (permalink)  
Old 08-18-09, 11:14 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
^- matches anything except dashes, which includes other characters than aren't allowed in domain names.
Code:
/^[0-9a-z][0-9a-z-]{1,18}[0-9a-z]$/i
... min length 3, max length 20.

Make sure to verify this on the server side as well. Otherwise it'll only be slighlty more secure. You can use the same regex pattern as above in preg_match().
Reply With Quote
  #7 (permalink)  
Old 08-18-09, 09:37 PM
Mythvn Mythvn is offline
Wannabe Coder
 
Join Date: Jan 2006
Posts: 111
Thanks: 2
Thanked 0 Times in 0 Posts
I just want to validate on the client side first to reduce http request to the server. Anyway, thanks so much for your help.
Reply With Quote
  #8 (permalink)  
Old 08-19-09, 03:17 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Well you should do both.

Most people have Javascript enabled, and this script would prevent extra HTTP requests for most. But not validating on the client side will allow your users to mess up your site.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Subdomain redirect rodrico101 Web Servers 0 08-30-08 08:31 PM
Subdomain 404 error pages Barnz1986 Web Servers 6 05-26-06 09:38 AM
Free phpBB hosting - NO Ads / 100+ Themes / Unlimited BW / Custom Mods / Subdomain ezForum General Advertisements 0 01-29-05 03:06 AM
automatic subdomain csmaster2005 PHP 4 07-25-04 03:11 PM
Disable form fields to be submitted RickyRod JavaScript 2 05-24-04 10:15 AM


All times are GMT -5. The time now is 01:54 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.