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

html - Submit Form Without Javascript - Stack Overflow

programmeradmin0浏览0评论

I have a form that relies on javascript to do all the client side form validation. The form is submitted via javascript if all the fields are correctly filled in as opposed to using a standard "submit" button:

<button name="button" type=button onclick="validateReqFields('mForm', 'username, password, compPassword, firstName, lastName');">Register</button>

document[formName].submit();

If the client has javascript disabled, all of the form validation is done server side (its actually performed again regardless but that doesn't really matter). The problem lies with using a button with a type of button instead of submit. It works perfect with javascript, but how do I get around this when javascript is not available? If I use a submit button along with the javascript then it submits the form with each button press and doesn't work properly.

I have a form that relies on javascript to do all the client side form validation. The form is submitted via javascript if all the fields are correctly filled in as opposed to using a standard "submit" button:

<button name="button" type=button onclick="validateReqFields('mForm', 'username, password, compPassword, firstName, lastName');">Register</button>

document[formName].submit();

If the client has javascript disabled, all of the form validation is done server side (its actually performed again regardless but that doesn't really matter). The problem lies with using a button with a type of button instead of submit. It works perfect with javascript, but how do I get around this when javascript is not available? If I use a submit button along with the javascript then it submits the form with each button press and doesn't work properly.

Share Improve this question asked Oct 17, 2011 at 3:01 ryandlfryandlf 28.6k37 gold badges110 silver badges164 bronze badges 2
  • 1 You should use the <input type="submit" value="Submit" onclick="validateReqFields(....)" /> – Pav Commented Oct 17, 2011 at 3:05
  • 2 Please see my comment below. I cannot do this. It states that clearly in the question. – ryandlf Commented Oct 17, 2011 at 3:08
Add a comment  | 

4 Answers 4

Reset to default 5

Use a submit button instead of the "button" button, and then have your validateReqFields function return false if the form is not valid.

Then take out the form submit from the validateReqFields if you like.

This way

  • If the form is valid, then the button click will bubble and the form will submit
  • If the form is invalid, then the javascript will cancel the button click
  • If javascript is disabled, then it will be submitted to the server as a fallback

Change the type attribute to submit.

<button name="button" type="submit" ... />

Use the <noscript></noscript> tags to define a "normal" submit-button when javascript is disabled.

You could maybe use the <noscript> tag and encapsulate the above code with the button type as submit. If the client has js, the code inside the noscript will be ignored.

发布评论

评论列表(0)

  1. 暂无评论