Shreyas Achar
Shreyas Achar

Reputation: 1435

Javascript validation in php

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.

Upvotes: 2

Views: 58

Answers (2)

rajesh
rajesh

Reputation: 2523

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>

Upvotes: 2

Sujal Patel
Sujal Patel

Reputation: 2532

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

Upvotes: 1

Related Questions