Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate domain name using regex?

This is my code for validating domain name.

function frmValidate() {
    var val = document.frmDomin;
    if (/^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\.[a-zA-Z]{2,}$/.test(val.name.value)) {
    }
    else {
        alert("Enter Valid Domain Name");
        val.name.focus();
        return false;
    }
}

and

<form name="frmDomin" action="" method="post" onsubmit="return frmValidate();">
Domain Name : <input type="text" value="" id="name" name="name"  />
</form>

Now I entered http://devp1.tech.in and it alert the message. I want to enter sub domain also. How to change this? I should not get alert.

like image 349
PNG Avatar asked Sep 29 '14 06:09

PNG


People also ask

How do you validate a RegEx pattern?

RegEx pattern validation can be added to text input type questions. To add validation, click on the Validation icon on the text input type question.

Is RegEx used for validation?

The Validation (Regex) property helps you define a set of validation options for a given field. In general, this field property is used to perform validation checks (format, length, etc.) on the value that the user enters in a field. If the user enters a value that does not pass these checks, it will throw an error.

How do I know if my FQDN is valid?

The validation of FQDN object is performed using RegEx pattern ^([a-zA-Z0-9. _-])+$ by the system to determine if a given hostname uses valid characters. Where ^ specifies start of a string, and $ specifies end of a string and + indicates one or more strings in the Round Brackets. [a-zA-Z0-9.


1 Answers

Try this:

^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+$

Demo

like image 115
walid toumi Avatar answered Sep 29 '22 22:09

walid toumi