I have this javascript:
document.write('<a HREF="javascript:return validateForm();' + thisform + '.submit();void(0);"');
I also have the following function, referencing a form on my website.
function validateForm()
{
var y=document.forms["myForm"]["email"].value;
if (y==null || y=="") {
alert('Please enter your email to continue.');
return false;
}
}
Now how do I get that return in the HREF to work!? It won't return false in the case that the input equals "" and validateform() returns false.
I have this javascript:
document.write('<a HREF="javascript:return validateForm();' + thisform + '.submit();void(0);"');
I also have the following function, referencing a form on my website.
function validateForm()
{
var y=document.forms["myForm"]["email"].value;
if (y==null || y=="") {
alert('Please enter your email to continue.');
return false;
}
}
Now how do I get that return in the HREF to work!? It won't return false in the case that the input equals "" and validateform() returns false.
Share Improve this question edited Mar 13, 2012 at 19:01 Charles 51.5k13 gold badges106 silver badges144 bronze badges asked Sep 14, 2011 at 1:55 Arden SArden S 111 silver badge3 bronze badges 2-
I'm not very familiar with JS inside the
href
, but won't returningvalidateForm()
at all prevent the form from being submitted? jsfiddle/fpYXY – sdleihssirhc Commented Sep 14, 2011 at 2:04 - It doesn't! the php carries on as usual! The problem is I use an image as a submit, and it uses an href as well so that I can have a rollover image change. The href submits the form and returns the validate, but only before running the php anyway. – Arden S Commented Sep 14, 2011 at 4:38
4 Answers
Reset to default 2Your code:
document.write('<a HREF="javascript:return validateForm();' +
thisform + '.submit();void(0);"');
Seems inplete. If you are using a link, then the href should link to a useful resource. If not, use a button or some other UI object that clearly indicates what will occur.
Returning false using the javascript pseudo protocol for the href attribute does nothing. The void statement does nothing at all (that is the point of void). Also, void is an operator so no need to include the grouping operator ().
You can't use an A element like this to conditionally submit the form, you really should be using the form's submit event to conditionally allow or prevent the submit. You can do:
document.write(
'<a href="http://useful." onclick="return validateForm(' +
thisform + ');">do stuff<\/a>');
Then within the validateForm function you can conditionally call the submit method of the form.
If validateForm returns false, the browser will not follow the link.
A far better strategy is to put the listener on the form:
<form onsubmit="return validateForm(...)" ...>
If validation fails, return false and the submit will be cancelled. Otherwise, the submit will continue.
In order to control the form submission appropriately, you should have this kind of solution:
document.write('<a href="#" onclick="submitMyForm(' + thisForm + ');"');
Then have this JS function:
function submitMyForm(theForm) {
var y = theForm["email"].value;
if (y==null || y=="") {
alert('Please enter your email to continue.');
} else {
theForm.submit(); // will only submit the form if it's valid
}
}
Why do you have
alert("Not a valid e-mail address.");
document.location = "http://www.snarephoto./about/";
stopnow()
return false;
I think you should only have..
alert("Not a valid e-mail address.");
return false;
Also, what does stopnow do in your case?
I don't know why you don't have to return a function inside a href, but this is really non remended.
I'm not really answering your question. But I can not figure out what the situation would be truly necessary to use it out instead of Javascript OOP.
I strong remend to adapt your code to a non obstrusive way like that.
Body:
<p><a href="#" id="myLink">Item 1</a></p>
Script
<script type="text/javascript">
var myFunction=function(){
//do something
alert('You clicked me!');
alert('This function is inside '+this.innerHTML+' Link');
this.style.backgroundColor='Yellow';
}
document.getElementById('myLink').onclick=myFunction;
</script>
This not call the function directly, a reference in memory is created (like pointers in C) in the Virtual Machine. and the most important NONE EVAL is used, and are much faster then inline eval.