1

The code below validate two nameserver textbox. As you can see there is redundancy in the javascript code. // validate textbox 1 and // validate textbox 2. Is there anyway I could just use one script.. you know I just want to use 1 validation function to validate two textbox. I'm sorry for my English I hope you all can understand me. Thank you.

    <script type="text/javascript">

// validate textbox 1
     function validate_domain(){
       var nameserver1 = document.getElementById('nameserver1').value;
       var domain_array = nameserver1.split('.');

      var domain = domain_array[0];
    //This is reguler expresion for domain validation
      var reg = /^([A-Za-z0-9])+[A-Za-z0-9-]+([A-Za-z0-9])$/;

    if(domain == ''){
         alert("Please enter the domain name"); 
         document.getElementsById('nameserver1').focus();
         return false; 
    } 

    if(reg.test(domain) == false){
       alert("Invalid character in domain. Only letters, numbers or hyphens are allowed.");
      document.getElementsById('nameserver1').focus();
       return false;
    }

    }



    // validate textbox 2
     function validate_domain(){
       var nameserver1 = document.getElementById('nameserver1').value;
       var domain_array = nameserver2.split('.');

      var domain = domain_array[0];
    //This is reguler expresion for domain validation
      var reg = /^([A-Za-z0-9])+[A-Za-z0-9-]+([A-Za-z0-9])$/;

    if(domain == ''){
         alert("Please enter the domain name"); 
         document.getElementsById('nameserver2').focus();
         return false; 
    } 

    if(reg.test(domain) == false){
       alert("Invalid character in domain. Only letters, numbers or hyphens are allowed.");
      document.getElementsById('nameserver2').focus();
       return false;
    }

    }
    </script>

 <fieldset class="inlineLabels">

            <label for="name">Nameserver 1</label>
            <input type="text" class="textInput" maxlength="255" size="30" value="" id="nameserver1" name="nameserver1">

            <label for="data">Nameserver 2</label>
            <input type="text" class="textInput" maxlength="255" size="30" value="" id="data" name="nameserver2">

        </fieldset>

          <button onclick="validate_domain(); submitForm('page1','directpage.php');" value="Validate" name="btn_validate" type="button" class="positive iconstxt icoPositive"><span>Save</span></button>
2
  • Okay, I think i'm finally done messing with my answer now, it should be drop in ready. Commented Feb 8, 2012 at 19:06
  • I appreciate everybody help but for times being none of the provided code fully functional for me. Thanks again. Commented Feb 8, 2012 at 19:45

5 Answers 5

1

Well here is another approach:

function ValidateDomain(){
    function CheckForBlank(domain, textBox){
        if(domain == ''){
             alert("Please enter the domain name"); 
             document.getElementsById('nameserver1').focus();
             return false; 
        } 
    }
    function CheckForFormat(domain, textBox){
        if(reg.test(domain) == false){
            alert("Invalid character in domain. Only letters, numbers or hyphens are allowed.");
            document.getElementsById('nameserver1').focus();
            return false;
        }
    }
    function GetDomainName(inputId){
        var serverName = document.getElementById(inputId).value,
            domain_array = serverName.split('.');

        return domain_array[0];
    }
    var nameserver1 = GetDomainName('nameserver1'),
        nameserver2 = GetDomainName('nameserver2'),
        nameServerInput1 = document.getElementsById('nameserver1');
        nameServerInput2 = document.getElementsById('nameserver2');

    if (CheckForFormat(nameserver1,nameServerInput1) && CheckForBlank(nameserver1,nameServerInput1) 
        && CheckForFormat(nameserver2,nameServerInput2) && CheckForBlank(nameserver2,nameServerInput2)){
        //This means you are valid
        return {
            name1:nameserver1,
            name2:nameserver2 
        }
    }
    return false;
}
Sign up to request clarification or add additional context in comments.

2 Comments

I paste your script and click empty button, alertbox appear but when I filled the textbox with data the variable is not submitted to target pages :(
You can't just copy and paste what I've written. Where I have //This means you are valid you have to do what you need this function to do. Probably return both of the nameserver values. You could return something like {name1:nameserver1, name2:nameserver2} Check out my updated answer.
1

Maybe like this:

    <script type="text/javascript">

function validate_domain(){

           validateTextBox('nameserver1');
            validateTextBox('nameserver2');
}

        function validateTextBox(tbName){

               var nameserver1 = document.getElementById(tbName).value;
               var domain_array = nameserver1.split('.');

              var domain = domain_array[0];
            //This is reguler expresion for domain validation
              var reg = /^([A-Za-z0-9])+[A-Za-z0-9-]+([A-Za-z0-9])$/;

            if(domain == ''){
                 alert("Please enter the domain name"); 
                 document.getElementsById(tbName).focus();
                 return false; 
            } 

            if(reg.test(domain) == false){
               alert("Invalid character in domain. Only letters, numbers or hyphens are allowed.");
              document.getElementsById(tbName).focus();
               return false;
            }

            }

        }
            </script>

1 Comment

I paste your script and click empty button, unfortunately no alertbox appear :(
1

Saw this one at work. Played with it at home. Here's a working jsFiddle. Yes, it's overly complex, but I detest alert('annoying pop-up');.

I commented the source, so you would better understand why I wrote it like that.

sg522: It may not copy/paste into your code and work, but I don't know what the rest of your code is. Neither are we here to write your code for you. We are here to help you learn and become a more experienced programmer.

Please let us know if you have questions.

Happy coding!

UPDATE: Modified jsFiddle to work with Opera and Firefox 10. Neither Opera or Firefox apparently allow cloneNode to be called without parameters now. Opera also apparently does not allow chained variable declarations.

5 Comments

If I understand correctly I will see the output in <div id="nameserverInputs"></div> correct? I test the code with empty input at jsFiddle and it don't work?
You understand correctly. It works for me in Google Chrome. What browser are you using to test it with?
It turns out it didn't work for Opera 11.61 and Firefox 10.0 but works with IE 9.0 and Safari 5.1. Is there anyway you could make it cross browser compatible?
What a shame. I have test your solutions with my app and it works beautifully but it not cross browser compatible.
Modified jsFiddle to work with Opera and Firefox 10. Neither Opera or Firefox apparently allow cloneNode to be called without parameters now. Opera also apparently does not allow chained variable declarations.
0

Take the id of the element you want to validate as a parameter of your function.

// validate textbox
 function validate_domain(serverName){
   var server = document.getElementById(serverName).value;
   var domain_array = server.split('.');

  var domain = domain_array[0];
//This is reguler expresion for domain validation
  var reg = /^([A-Za-z0-9])+[A-Za-z0-9-]+([A-Za-z0-9])$/;

if(domain == ''){
     alert("Please enter the domain name"); 
     document.getElementsById(serverName).focus();
     return false; 
} 

if(reg.test(domain) == false){
   alert("Invalid character in domain. Only letters, numbers or hyphens are allowed.");
  document.getElementsById(serverName).focus();
   return false;
}

}

1 Comment

I paste your script and click empty button, unfortunately no alertbox appear :(
0
    <script type="text/javascript">

// validate textbox
     function validate_domain(ele){
       var nameserver = ele.value;
       var domain_array = nameserver.split('.');

      var domain = domain_array[0];
    //This is reguler expresion for domain validation
      var reg = /^([A-Za-z0-9])+[A-Za-z0-9-]+([A-Za-z0-9])$/;

    if(domain == ''){
         alert("Please enter the domain name"); 
         ele.focus();
         return false; 
    } 

    if(reg.test(domain) == false){
       alert("Invalid character in domain. Only letters, numbers or hyphens are allowed.");
      ele.focus();
       return false;
    }

    }
    </script>

 <fieldset class="inlineLabels">

            <label for="name">Nameserver 1</label>
            <input type="text" class="textInput" maxlength="255" size="30" value="" id="nameserver1" name="nameserver1">

            <label for="data">Nameserver 2</label>
            <input type="text" class="textInput" maxlength="255" size="30" value="" id="data" name="nameserver2">

        </fieldset>

          <button onclick="validate_domain(document.getElementById('nameserver1')); validate_domain(document.getElementById('nameserver2')); submitForm('page1','directpage.php');" value="Validate" name="btn_validate" type="button" class="positive iconstxt icoPositive"><span>Save</span></button>

4 Comments

I paste your script and click empty button, alertbox appear but when I filled the textbox with data the variable is not submitted to target pages :(
Well, it must be your submitForm function... Otherwise, you can create a form (it would be better actually), and adding to it onsubmit="if (!validate_domain(document.getElementById('nameserver1')) || !validate_domain(document.getElementById('nameserver2')) ){return false;}"
<form onsubmit="if (!validate_domain(document.getElementById('nameserver1')) || !validate_domain(document.getElementById('nameserver2')) ){return false;}" method="post" action="dns/dns_ns_wizard_pros.php"> <input type="submit" value="submit" name="submit"> </form> Is this OK??
It's not correct. I try it and the data is still not submitted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.