0

Here what the textbox result looks like,

Please add the following DNS entries
144.68.238.87   name.domain 
144.68.238.88   name.domain 
144.68.238.89   name.domain

The goal is to validate name.domain by making sure that the user replace name.domain to server name on textbox before submit it. If the user doesn't replace name.domain with their server name, then it will send alert message and return false until user replace it correctly.

Here is my codes,

function DomainValidate() {

        var arrayOfLines = document.getElementById('txt').value.split('/n');

        arrayOfLines.shift(); //use shift to skip the first line

        for (i = 0; i < arrayOfLines.length; i++) {

           //somewhere here need to split to get name.domain and then verify it
            var domainName = arrayOfLines[i].split(" ", 2);

            if(domainName.Equals("name.domain")
            {
               alert("You must replace name.domain to your new server name");
               return false;
            }
        }

}

I am not sure if these are correct since I couldn't debug the javascript.

2 Answers 2

1

First issue I can see is that your character for the newline is incorrect. It should be \n not /n. Second issue I see is that i is a global variable, when it should be local. Third issue is that arrayOfLines[i].split(' ', 2); returns an array, but you are treating it like it returns a string on the next line if (domainName.Equals('name.domain').

With those corrections your code would look more like this:

function domainValidate() {
    var arrayOfLines = document.getElementById('txt').value.split('\n');

    arrayOfLines.shift(); //use shift to skip the first line

    for (var i = 0; i < arrayOfLines.length; i++) {
        var line = arrayOfLines[i].trim();
        // Grab the second part of the split line, which represents the domain name
        var parts = line.split(' ');
        var domainName = parts[parts.length - 1];

        if (!domainName || domainName === 'name.domain') {
            alert("You must replace name.domain to your new server name");
            return false;
        }
    }
}

As far as I can tell without testing, this should work as expected. The best way to test this though is with jsfiddle. Add your html and this script and call it to see if it produces the expected result.

Sign up to request clarification or add additional context in comments.

4 Comments

I was testing and debugging javascript, the domainName receive empty string which it was suppose to be name.doman that I left it out or even I did replace with server name. However the ArrayOfLines do have 144.68.238.87 name.domain. I am suspecting it had to do with splitting wrong way? Was it has to do specific whitespace or it shouldn't matter how many whitespace to get second part?
I updated the code after testing it in jsfiddle. Should work now for you.
Another issue is that the loop continue to run after the last name.domain line which it probably get empty string from next empty line or so. Do you know how to prevent when reach to last line and then stop it from reading the empty line.
Oh hey, sorry, all you need to do is check ahead to the next element in arrayOfLines using arrayOfLines[i+1] and validate that the line is non-empty. Make sure you verify that the index yielded by i+1 is less than arrayOfLines.Length though, or you'll get an error.
0

Easiest way that I think

Suppose the id of textbox is domainTxt

src = document.getElementById("domainTxt");
if(verifyInput(src.value)){
    //submit your form here
} else
{
    return false;
}

function verifyInput(txtVal){
    if(txtVal.indexOf("name.domain") >-1){
        return false;
    }else {
        return true;
    }
}

Comments

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.