-1
    <script>
            function checkName(){
                var tmpName = document.getElementById("name");
                if( tmpName.value.length == 0 ){
                    alert( "Name cannot be empty" );
                    return false;
                }
                else
                    return true;
            }


            function confirmForm( formObj ){
                var nameBool = new Boolean();
                var errorName = "";


                nameBool = checkName();

                if( nameBool == false )
                    errorName = "Invalid data in input name !";

                return window.alert( "You have this error : \n " + errorName + "\n" + errorID ); 
            }

</script>
<form name=reviewform onsubmit="return confirmForm(reviewForm)" method=POST >
            <p>Name : <input type="text" id="name" ></p>

            <p>ID   : <input type="text" id="id" ></p>

            <input type="submit" value="Click here!">
</form>

my problem is why my function cannot run ??? isn't something wrong? can any senior teach me where the place i wrong at??

i already editited and plus that form name and the function how it's work

i don't know isn't my implementation wrong or what

Chirs Lim
  • 9
  • 2

1 Answers1

1

Both functions, though they need some work, and you clearly need to brush up on your JS knowledge, can run, you just have to call them. As it stands, you've just defined 2 functions. Nothing else.

Other issues, line/line:

//second line:
var tmpName = document.getElementById("name");

Here, you can't be sure this element is already leaded, perhaps the DOM isn't ready yet, so be careful, just wrap your entire code in a handler:

window.onload = function()
{
    //your code here, this gets executed after the page is fully loaded
};

Next, don't think of JS as some sort of Java-for-browsers, it's a completely different animal. Stuff like:

var nameBool = new Boolean();
var errorName = "";

Is best written as declaring variables, but not assigning anything:

var nameBool, errorName;

If you want to be sure the nameBool is a boolean, just assign like so:

nameBool = !!checkName();//double bang

It also looks like you're trying to validate a form or handle a submit event of sorts. Why not use addEventListener for that? or, if you insist:

document.getElementById('formId').onsubmit = function(e)
{
    //get event object:
    e = e || window.event;
    //this references form, as does (e.target || e.srcElement), you can access the elements it contains, and check them one by one
};
Community
  • 1
  • 1
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149