2

I have a custom template in wordpress. In that custom template I have a form like this:

<label style="font-size:12px;"> First Name:</label>
<input type="text" id="RF_FName" name="RF_FName" />
<input  id="RegisterUser" type="submit" Value="Register" name="RegisterUser" onclick="validate();" />

I'm validating all the fields, like:

<script>
    function validate() {
        if (document.getElementById("RF_FName").value == "") {
        alert("Enter your First Name");
        document.getElementById("RF_FName").focus();
        exit();
        function exit() {
        var i;
            window.addEventListener('error', function (e) {e.preventDefault(); e.stopPropagation(); }, false);
            var handlers = [
                'copy', 'cut', 'paste',
                'beforeunload', 'blur', 'change', 'click', 'contextmenu', 'dblclick', 'focus', 'keydown', 'keypress', 'keyup', 'mousedown', 'mousemove', 'mouseout', 'mouseover', 'mouseup', 'resize', 'scroll',
                'DOMNodeInserted', 'DOMNodeRemoved', 'DOMNodeRemovedFromDocument', 'DOMNodeInsertedIntoDocument', 'DOMAttrModified', 'DOMCharacterDataModified', 'DOMElementNameChanged', 'DOMAttributeNameChanged', 'DOMActivate', 'DOMFocusIn', 'DOMFocusOut', 'online', 'offline', 'textInput',
                'abort', 'close', 'dragdrop', 'load', 'paint', 'reset', 'select', 'submit', 'unload'
            ];
            function stopPropagation (e) {
                e.stopPropagation();
                // e.preventDefault(); // Stop for the form controls, etc., too?
            }
            for (i = 0; i < handlers.length; i++) {
                window.addEventListener(handlers[i], function (e) {stopPropagation(e); }, true);
            }

            if (window.stop) {
                window.stop();
            }

            throw '';
        }
</script>

I have tried with return; instead of exit. Still it's going to server side.

But when I click submit button the code is going to functions.php. I want to stop the code here while validating and should not enter the action page.
Any help appreciated.
Thanks.

1
  • 1
    use return false instead of exit and in onclick change to return validate() Commented Jan 18, 2017 at 7:24

2 Answers 2

2

add the validate function to onsubmit event of the form. Then if you return false from the valdate method it wont got to action page.

<form onsubmit="return validate();">
<label style="font-size:12px;"> First Name:</label>
    <input type="text" id="RF_FName" name="RF_FName" />
  <input  id="RegisterUser" type="submit" Value="Register" name="RegisterUser"  /> 
</form> 

<script>
function validate() {
 if (document.getElementById("RF_FName").value == "") {
 alert("Enter your First Name");
 document.getElementById("RF_FName").focus();
 return false;
 }
}
</script>
Sign up to request clarification or add additional context in comments.

Comments

1

Try this :

<label style="font-size:12px;"> First Name:</label>
        <input type="text" id="RF_FName" name="RF_FName" />
  <input  id="RegisterUser" type="submit" Value="Register" name="RegisterUser" onclick="return validate();" />


function validate() {
if (document.getElementById("RF_FName").value == "") {
    alert("Enter your First Name");
    document.getElementById("RF_FName").focus();
    return false;
}
}

i hope it's works

1 Comment

show my edited answer ..... in onclick event write return before the function and then use return false

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.