0

I've created a form using jqBootstrapValidation.js to validate it.

However I can't seem to get the regex for my FQDN format field to work.

<input class="form-control"
type="text"
name="cn"
id="commonname"
data-validation-regex-regex="/^(?=.{1,254}$)((?=[a-z0-9-]{1,63}\.)(xn--)?[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63}$/i"
data-validation-regex-message="Must enter a vaild FQDN" required>

It invalidates any FQDN I enter.

I know that there are a few different formats to regular expressions.

Am I using the wrong formatting for javascript?

Elijah Paul
  • 281
  • 4
  • 22
  • Why bother with a regex when you can just do an nslookup? https://www.google.com/search?q=javascript+nslookup – James Sumners Nov 14 '13 at 19:38
  • But this won't work unless the domain is already registered and DNS is setup right? – Elijah Paul Nov 14 '13 at 20:26
  • So if I enter "a.b.cd" is that a valid FQDN? It'll match whatever expression you cook up. Point being, you really won't know it's a valid entry until you try to resolve it. – James Sumners Nov 14 '13 at 20:57
  • I see. I think I should edit/clarify my question. I'm looking to validate the FQDN format rather than an actual FQDN. – Elijah Paul Nov 14 '13 at 21:43
  • I originally looked at this [regular-expression-to-validate-fqdn-in-c-sharp-and-javascript](http://stackoverflow.com/questions/17986371/regular-expression-to-validate-fqdn-in-c-sharp-and-javascript) – Elijah Paul Nov 14 '13 at 21:45
  • Maybe something wrong. Change the regex to force it to fail `/(?!)/` if it doesn't fail, something is not hooked up. If it does fail try removing the double quotes in the att-val. Not working? Cut and paste this as the attr-val - `"^(?=.{1,254}$)((?=[a-z0-9-]{1,63}\\.)(xn--)?[a-z0-9]+(-[a-z0-9]+)*\\.)+[a-z]{2,63}$"` –  Nov 14 '13 at 21:46
  • Or just double escape the literal dots. –  Nov 14 '13 at 21:53
  • @sln Thanks. I tried `/(?!)/` and it did fail. Also tried removing the quotes and double escaping. But it still fails. Lost as to why. – Elijah Paul Nov 15 '13 at 15:53

1 Answers1

0

To help debug, try 1 - 6 below and report PASS/FAIL on usage.
Enter data that remotely should match.

 1.     "/(?!)/"                      // Should FAIL
 2.     "/^(?=.{1,254}$)/"            // Should PASS
 3.     "/(?=[\S\s]{1,254})/"         // Should PASS


 // 4 & 5  have \. replaced with [.]
 // and added (?i)
 // (If JS doesn't support (?i) modifiers, remove them
 // -------------------------------------------------------

     // this has no lookaheads nor anchors
     4.      "/(?i)((xn--)?[a-z0-9]+(-[a-z0-9]+)*[.])+[a-z]{2,63}/"

     // this has anchors, but no lookaheads
     5.      "/^(?i)((xn--)?[a-z0-9]+(-[a-z0-9]+)*[.])+[a-z]{2,63}$/"

     // this has anchors and lookaheads
     6.      "/^(?i)(?=.{1,254}$)((?=[a-z0-9-]{1,63}[.])(xn--)?[a-z0-9]+(-[a-z0-9]+)*[.])+[a-z]{2,63}$/"
  • Thanks @sln . Have tried all of the above but with no luck. I've contacted the guy who wrote the script asking for some further guidance. – Elijah Paul Nov 17 '13 at 05:50
  • I've found that the following Regex works: `(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]).)*([A-Za-z]|[A-Za-z][A-Za-z0-9-]*[A-Za-z0-9])` But I'm not sure why? Is this a different format to the ones above? – Elijah Paul Nov 18 '13 at 01:13