Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7438397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:32:56+00:00 2026-05-29T10:32:56+00:00

The code below validate two nameserver textbox. As you can see there is redundancy

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T10:32:57+00:00Added an answer on May 29, 2026 at 10:32 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The below code to validate TextBox is working fine onkeypress but when I use
I have some very simple code below that I can't get to validate on
How can we validate the below code ? I want validate the input named
I am using the code below to validate integers and floats in ASP.NET, but
Please see the code below. Basically, when the user creates an object of this
I'm using the code below with the jQuery Validate plug-in, however I'm receiving the
Below is the link to the code that has the two RAdEditors I am
I have a JSF page with two forms (pseudo-code below). First form is for
I have written the below code to validate the value in the string before
I am using the below code to validate a user's input: function validateAddress(){ //if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.