HTML form action and onsubmit issues

余生颓废 提交于 2019-11-26 17:39:59

You should stop submit procedure by returning false on onsubmit callback.

<script>
function checkRegistration(){
    if(!form_valid){
        alert('Given data is not correct');
        return false;
    }
    return true;
}
</script>
<form onsubmit="return checkRegistration()"...

UPDATE:

here you have fully working example, form will submit only when you write google into input, otherwise it will return error:

<script>
function checkRegistration(){
    var form_valid = (document.getElementById('some_input').value == 'google');
    if(!form_valid){
        alert('Given data is incorrect');
        return false;
    }
    return true;
}
</script>
<form onsubmit="return checkRegistration()" method="get" action="http://google.com">
    Write google to go to google..<br/>
    <input type="text" id="some_input" value=""/>
    <input type="submit" value="google it"/>
</form>

Try

onsubmit="return checkRegistration()"

try:

onsubmit="checkRegistration(event.preventDefault())"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!