Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 177
  • Last Modified:

how to write java script validate function

Hi:

Currently I am working on an customer order form. The email address should be entered when the user submits. So I need add something like "input type=submit value=submit onclick="validate()"  " right? My question is how to write the validate() so that the user won't submit?
0
ly_uwo
Asked:
ly_uwo
1 Solution
 
boonlengCommented:
You need to return true or false in the javascript function.

Example:
<script>
function validate() {
    if (no error) {
        return true;
    } else {
        return false;
    }
}
</script>

<form onsubmit="return validate()">

Note: is best perform validation on onSubmit instead of onclick on the button. This is to cater for user press 'Enter' key.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now