开发者

Javascript validation for a method calling onLoad()

I have a form with few text fields and drop downs.

Iam calling a method onLoad,validating those text fields and dropdowns in that method.

The user must select one of the options from the dropdown.

The dropdown should开发者_Python百科 not be empty, hence I have a condition like if(empList==" "), if it is empty an alert will popup saying that shouln't be empty.

Now,the actual problem is that the alert is popping-up once I enter into that page, before I select any option as I have given that in onLoad.

How should I validate that field?? (I should compulsary call that method onLoad)


What you need to do is to instead call your validation method from like the onchange event of the select tag if you want to validate as soon as changes are made to the selection.

Actually, the best thing would be to put call the your validation function in the onclick event of a button tag used to submit the form, and in that case, ensure to not set its type to submit but instead set it to button, so that the form isn't automatically submitted before you make validation.

Then in the validation method, if all is ok, then manually submit the form by doing this:

document.forms["myform"].submit();


You have to put all the validation on the Submit of the form.

<FORM 
     ACTION="../...jsp"
     NAME="testform" 
     onSubmit="return validate()"
     >
</form>


Instead of using obstrusive popup, you'd better use asynchronous validation that will display alert or info if the form is not filled properly.

Some framework plugins can do it natively : have a look here for instance : http://docs.jquery.com/Plugins/validation

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜