1

I am writing a script which will verify username . I am able to put check on user name. In case username is not fulfilling criteria then I am throwing error.

<script>
    //<![CDATA[
    function visitPage() {

        if (validateUsername()) {
            var div = document.getElementById("totalpricecheck");
            var spans = div.getElementsByTagName("span");

            var totalprice = spans[3].innerHTML;
            var name = document.getElementById("name").value;
            alert(name);


        }


    }

    function validateUsername(fld) {
        var fld = document.getElementById("name").value;
        var error = "";
        var illegalChars = /\W/; // allow letters, numbers, and underscores

        if (fld.value == "") {
            //fld.style.background = 'Yellow';
            error = "You didn't enter a username.\n";
            document.getElementById("nmessage").innerHTML = "You didn't enter a username.\n";
            // $("#nmessage").fadeOut(3000);
            // alert(error);
            return false;

        } else if ((fld.length < 5) || (fld.length > 50)) {
            //fld.style.background = 'Yellow';
            error = "The username is the wrong length.\n";
            document.getElementById("nmessage").innerHTML = "OOps!! The username is too short \n";
            // alert(error);
            return false;

        } else if (illegalChars.test(fld.value)) {
            //fld.style.background = 'Yellow';
            document.getElementById("nmessage").innerHTML = "The username contains Unsupported characters.\n";
            error = "The username contains Unsupported characters.\n";
            // alert(error);
            return false;

        } else {
            // fld.style.background = 'White';
        }
        return true;
    }
    return false;


    }

    // ]]>
</script>

I am trying to hide this error using fadeout effect as given in Hide div after a few seconds

setTimeout(function() {
    $('#mydiv').fadeOut('fast');
}, 1000); // <-- time in milliseconds

but am not getting how can I use jQuery method in JavaScript for error message removal. someone suggest me what are possible option I have to get desired effect.

Community
  • 1
  • 1
nand
  • 517
  • 2
  • 13
  • 29

2 Answers2

2

Judging by your code here:

document.getElementById("nmessage").innerHTML ="The username contains Unsupported characters.\n";

Your setTimeout function is not calling the right div to hide. It should look like this:

setTimeout(function() {
    $('#nmessage').fadeOut('fast');
}, 1000); // <-- time in milliseconds

I also did not see this implemented in your script at all so add that setTimeout function call after you change the innerHTML.

IfTrue
  • 489
  • 8
  • 25
  • Someone else mentioned making sure you have the jquery library included - noticed there may be some vagueness around if you know that needs to be included or not so just in case I will remind you to check that as well. – IfTrue Aug 17 '15 at 17:12
2
  1. As @iFTrue mentioned in the other post, correct div ID has to be provided.
  2. As @chitrang mentioned in comment jQuery library has to be included in the page if its not already done.

To inlude jQuery from CDN use the below code. Paste it inside head tag

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

You should set the timer inside visitPage()

function visitPage() {
               if(validateUsername()) {
                      var div = document.getElementById("totalpricecheck");
                      var spans=div.getElementsByTagName("span");

                      var totalprice=spans[3].innerHTML;
                      var name = document.getElementById("name").value;
                      alert(name);      
               }
               else {
                    // show error message 
                    $('#nmessage').show();

                    // hide error message after 3 sec 
                    setTimeout(function() {
                       $('#nmessage').fadeOut('fast');
                    }, 3000); 
               }
            }

Update

I see that you are using jQuery only to hide and show the div. If you don't need the fadeOut animation effect, you can remove jQuery library and use the below code.

function visitPage() {
               if(validateUsername()) {
                      var div = document.getElementById("totalpricecheck");
                      var spans=div.getElementsByTagName("span");

                      var totalprice=spans[3].innerHTML;
                      var name = document.getElementById("name").value;
                      alert(name);      
               }
               else {
                    // show error message 
                    document.getElementById("nmessage").style.display = 'block';
                    // hide error message after 3 sec 
                    setTimeout(function() {
                        document.getElementById("nmessage").style.display = 'none';
                    }, 3000); 
               }
            }
kiranvj
  • 32,342
  • 7
  • 71
  • 76
  • In case he only views your answer and not mine: Please note kiranvj did not highlight the fact the ID was incorrect as well just so you know that needs corrected as well Nand. – IfTrue Aug 17 '15 at 17:11
  • 1
    Thanks @IfTrue, +1 I have mentioned that as well in the answer. – kiranvj Aug 17 '15 at 17:15
  • @kiranvj, yes its working but only for a single click. on second click it seems inactive even its not showing error message and also in case if i intent to verify two more fields let say email and phone no then how will I implement on them this time out function ? – nand Aug 17 '15 at 17:43
  • @Nand In that case you need to show the error message div again, I have updated the answer with that. – kiranvj Aug 17 '15 at 18:08
  • @kiranvj Yes, its working now .Please provide code snap in case I am adding two more field for verification . I tried in same manner **$('#emessage').show(); setTimeout(function() { $('#emessage').fadeOut('fast'); }, 3000); $('#pmessage').show(); setTimeout(function() { $('#pmessage').fadeOut('fast'); }, 3000);** but it didn't working – nand Aug 17 '15 at 18:59