最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript return true not working - Stack Overflow

programmeradmin1浏览0评论

i have used the following code for javascript validation, that return true or false depending on the condition

javascript block

function fnval()
{
 if(document.field.value == "")
 {
  alert("Invalid value");
   return false;
 }else{
   return true;
 }
}

Here is my HTML:

<Input type=submit name="sub" onClick="return fnval()">

Thus the js block checks if the field value is entered or not. If not it throws an alert message and return false, and hence the form does not get submitted.
But in case the value is not empty it returns a true, and still the form does not get submitted.

I have seen some queries asked by people where return false results in submission of the form. But this is exactly opposite.. and am not able to find a solution as of now.
Can anyone please help me on this?

i have used the following code for javascript validation, that return true or false depending on the condition

javascript block

function fnval()
{
 if(document.field.value == "")
 {
  alert("Invalid value");
   return false;
 }else{
   return true;
 }
}

Here is my HTML:

<Input type=submit name="sub" onClick="return fnval()">

Thus the js block checks if the field value is entered or not. If not it throws an alert message and return false, and hence the form does not get submitted.
But in case the value is not empty it returns a true, and still the form does not get submitted.

I have seen some queries asked by people where return false results in submission of the form. But this is exactly opposite.. and am not able to find a solution as of now.
Can anyone please help me on this?

Share Improve this question edited Nov 28, 2011 at 14:45 JMax 26.6k12 gold badges74 silver badges89 bronze badges asked Nov 28, 2011 at 14:41 user1069489user1069489 11 silver badge1 bronze badge 4
  • 5 Is your <input> element inside a <form> element? – Richard JP Le Guen Commented Nov 28, 2011 at 14:45
  • And you don't "throw" an alert message ;) – Richard JP Le Guen Commented Nov 28, 2011 at 14:48
  • 1 you should look at using jquery. Its a javascript library for working with elements on the page, just like you're attempting to do here. I'm not sure what document.field should return. Have a look into jquery, its so much easier and its cross browser patible. – Thomas Clayson Commented Nov 28, 2011 at 14:50
  • I suggest you check out VanadiumJS. It will make your life a hell of a lot easier. – Meisam Mulla Commented Nov 28, 2011 at 14:54
Add a ment  | 

5 Answers 5

Reset to default 3

Try getElementsByName:

function fnval()
{
 if(document.getElementsByName('field')[0].value == "")
 {
  alert("Invalid value");
   return false;
 }else{
   return true;
 }
}


getElementsByName doesn't have IE support though. Perhaps:

function fnval()
{
 if(findInput('field')[0].value == "")
 {
  alert("Invalid value");
   return false;
 }else{
   return true;
 }
}
function findInput(name) {
    var elements = document.getElementsByTagName('input'),
        length = elements.length,
        i = 0,
        results = [];
    for(i; i<length; i++) {
        if (elements[i].name === name) {
            results.push(elements[i]);
        }
    }
    return results;
}

You need to add the form name and the form value. Something like:

if ( document.formName.fieldName.value == "" )

For instance, with this kind of HTML:

<form method="post" onsubmit="">
Password: <input name="password" type="text" /><br />
</form>

The js:

if (document.form.password.value == "") {
    //empty
}

i suggest using onsubmit in the form, <form ... onsubmit="return fnval()">, try adding that and placing return false at the base of your function.

no matter what you do in js. but if you have filled action tag of form element element , the form will submit.

Syntax error:

type="submit"

not

type=submit
发布评论

评论列表(0)

  1. 暂无评论