0

I have very very stupid bug in javascript validation

let me i explain in code :

this is my form tag

 <form method="post" enctype="multipart/form-data" name="myForm" onsubmit="return validateForm()">
   <textarea id="content"   name="Body"><%= Model.Body %></textarea>
</form>

and this is my script :

 function validateForm(e) {
        debugger;
         var reviewMessage = $("[name='Body']").attr('value');
        //var overallValue = document.getElementsByClassName('checkbox overall icon-checkbox').prop.checked;
        if (reviewMessage.length < 100) {
            e.preventDefault();

           // $("Body").show();
            $('#bodyValidation').css({'display' : 'block'});
            return false;
        }
        return true;
    }

my problem is that when ever i click the button page will be submited ;

but i want to stop this action with javascript .

how can i do that?

salar
  • 710
  • 3
  • 6
  • 22

1 Answers1

2

Your selector is wrong.

Change

var reviewMessage = $("Body").val();

to

var reviewMessage = $("[name='Body']").val();

OR

var reviewMessage = $('#content').val();
Tushar
  • 85,780
  • 21
  • 159
  • 179
  • is it the main problem of submiting page ? – salar Jul 06 '15 at 13:20
  • @salar Yes, because you've `preventDefault()` and `return false` both in the condition that is using this value – Tushar Jul 06 '15 at 13:21
  • @salar If you look at your own code, you'll realize that if the length of `Body`'s value is shorter than 100, you'll do the validation, otherwise you simply submit. – Jeff Noel Jul 06 '15 at 13:22
  • @JeffNoel I update my code but still the same problem – salar Jul 06 '15 at 13:23
  • @salar You need to help us to help you. We're *guessing* what the `Body` element is, please include it in your code. Which browser are you testing your code in? If it is IE, `event.preventDefault()` will not work and you will need to use `event.returnValue = false` instead. Try debugging your code using the console and developper tools integrated within your browser. – Jeff Noel Jul 06 '15 at 13:26
  • @JeffNoel The condition is ok but my main problem is that page will be submited – salar Jul 06 '15 at 13:35