regular expression validation javascript for the domain name?


how to check a valid domain name and username with regular expression in javascript?

function validate()
 var patt1=new RegExp(/^[a-zA-Z0-9._-]+\\[a-zA-Z0-9.-]$/);

   var text= document.getElementById('text1').value;



but it does not work for me.

Use this:

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