Hi, Who can tell me why this dont work? <script language="JavaScript"> function fecha(){ fecha = document.getElementById('C7'); if(fecha.value == ""){ alert('You must indicate the approximate date of arrival'); return false; } </script> <form id="formulario" method="post" name="formulario" action="result.php" onSubmit="return fecha()"> <input name="C7" type="text" id="dateArrival" size="10"> <input id="send" name="send" type="submit" value="Submit" /> Code (markup): regards.
There are 2 errors: - the C7 input name ID should be C7 not dateArrival - you forgot to put a closing bracket for the function <script language="JavaScript"> function fecha(){ fecha = document.getElementById('C7'); if(fecha.value == ""){ alert('You must indicate the approximate date of arrival'); return false; } } </script> <form id="formulario" method="post" name="formulario" action="poll.php" onSubmit="return fecha()"> <input name="C7" type="text" id="C7" size="10"> <input id="send" name="send" type="submit" value="Submit" /> Code (markup):
Yes, thanks imocoder!! I forget the brackect.. I will pay for this work for my real form, check it: http://forums.digitalpoint.com/showthread.php?t=2003775#post15304693
you should use jslint to check syntax problems: Error: Problem at line 2 character 5: 'fecha' is a function. fecha = document.getElementById('C7'); Problem at line 3 character 21: Use '===' to compare with ''. if (fecha.value == "") { Problem at line 1 character 18: Unmatched '{'. function fecha() { Implied global: alert 4 Code (markup): This is a really bad bad example for a number of reasons, good thing you posted it so ppl can learn from it. when you define a function in javascript, eg. `function foo() { ... some code ... }', what the interpreter does is in fact: var foo; // (hoisting) ... foo = function() { // some code }; Code (javascript): variable declarations are appended to the window / global object (unless in a scope) so they become available via window.foo(); or even window['foo']() (which can be used to call functions dynamically from variables). what you also need to know is that javascript has a scope chain - this means, variables are declared at whatever level and inherited further down. hence, variables declared outside of all functions are becoming global (also available via window.varname) when you declare a variable inside a function, you HAVE to say 'var myname = 'dimitar';' failure to prefix this with var results in javascript declaring myname as a global window object property instead (equivalent to writing it as window.myname = 'dimitar'). let us review your code and check it run: function fecha() { fecha = document.getElementById('C7'); if (fecha.value == "") { alert('You must indicate the approximate date of arrival'); return false; } } window.onload = function() { fecha(); try { fecha(); } catch(e) { alert(e.message); } }; Code (javascript): view the demo here: http://www.jsfiddle.net/dimitar/p4Dbz/1/ on the first run, it all works fine. the second one - not so. this is because by saying fecha = document.getElementById('C7'); in the first pass, you are effectively saying, window.fecha = document.getElementById('C7'); and that overwrites your function (that also lives at window.fecha) - the function self-destroys so a second submit will always trigger an error. ALWAYS say var when you don't need to share variables outside of their function scope. if you need to reference global variables in functions, always reference them as window.varname to save the look-up time up the scope chain and locally. have fun