Wednesday, November 10, 2010

How to change value of form element before submit(Remove white space from a form variable before submit) - java script code

Here what i need to do is remove white spaces from the username variable before submit to server.This add advantage because server no need to do additional processing. This is very useful because in most cases before submit usernames and most of other parameters we have to remove white spaces

 

<script type="text/javascript">

    function checkform()
    {
        var username=document.getElementById('txtUserName').value;
        username=username.trim();
        document.getElementById('txtUserName').value=username;
        return true;
    }

      String.prototype.trim = function ()
     {
         return this.replace(/^\s*/, "").replace(/\s*$/, "");

//Regular expression is used to remove white space
     }

    </script>

Add above script to your page and modify your submit action as follows so it will call above methods when submitting

 

<form target="_self" method="POST" action="../admin/login_action.jsp" id="loginForm" onSubmit="return checkform()">

No comments:

Post a Comment