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

Empowering the Future of API Management: Unveiling the Journey of WSO2 API Platform for Kubernetes (APK) Project and the Anticipated Alpha Release

  Introduction In the ever-evolving realm of API management, our journey embarked on the APK project eight months ago, and now, with great a...