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?

4
  • stackoverflow.com/questions/12623244/… Commented Jul 6, 2015 at 13:17
  • Why don't you create jsfiddle of the problem? Commented Jul 6, 2015 at 13:37
  • @tushar im sorry but imot familiar with that Commented Jul 6, 2015 at 13:38
  • @salar Go to jsfiddle.net Add code in respective blocks, make sure it runs properly Commented Jul 6, 2015 at 13:40

1 Answer 1

2

Your selector is wrong.

Change

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

to

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

OR

var reviewMessage = $('#content').val();
Sign up to request clarification or add additional context in comments.

6 Comments

is it the main problem of submiting page ?
@salar Yes, because you've preventDefault() and return false both in the condition that is using this value
@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.
@JeffNoel I update my code but still the same problem
@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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.