1

I am trying to validate srv record with jquery preg_match

What i need: 'PriorNumber WeightNumber PortNumber Address/server'. This needs to be limited to max 5 parts in total.

For now i got a part working but only the last part is not working when i use a domain or ip address.

/^((\d+ \d+ \d+\w+.(?:\s+\w+)\1{0,1}$))/

Works = 100 1 4234 sipdsdfghj

Not working: 100 1 4234 sipdsdfghj.sadas

https://www.regextester.com/?fam=103214

Please help me on this

Full code validate

value = 100 1 4234 sipdsdfghj.asd

// Functie voor het valideren van srv record 'prioriteit gewicht poort doeladres'
jQuery.validator.addMethod("srvRecord", function(value, element) {
    if (value.indexOf(' ') >= 0) {

        console.log(value);

        if(value.match( /^(?<prior>\d+)\h+(?<weight>\d+)\h+(?<port>\d+)\h+(?:(?<ip>\d{1,3}(?:\.\d{1,3}){3})|(?<domain>\S+\.\S+))$/gm) !== null){
            return true;
        }

        return false
    } 
}, "* Amount must be greater than zero");
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Mitch Daniels
  • 33
  • 1
  • 7

1 Answers1

1

You may try this regex solution:

/^\d+(?:\s+\d+){2}\s+(?:\d{1,3}(?:\.\d{1,3}){3}|\S+\.\S+)$/

Note it does not really validate an IP or domain name, it only matches IP-like and domain-like substrings. See Regular expression to match DNS hostname or IP Address for details on a more precise IP regex.

Details

  • ^ - start of string
  • \d+ - 1+ digits
  • (?:\s+\d+){2} - 2 sequences of 1+ whitespaces and 1+ digits
  • \s+ - 1+ whitespaces
  • (?:\d{1,3}(?:\.\d{1,3}){3}|\S+\.\S+) - either of:
    • \d{1,3}(?:\.\d{1,3}){3} - 1 to 3 digits, and then 3 sequences of . and 1 to 3 digits
    • \S+\.\S+ - 1+ non-whitespace chars, . and again 1+ non-whitespace chars
  • $ - end of string.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563