Results 1 to 4 of 4

Thread: How to Replace blank fields with Javascript

  1. #1
    Join Date
    Dec 2008
    Posts
    112

    How to Replace blank fields with Javascript

    I wanted to set the validation on the field to check whether the data I have provide on the data and the data which i have already inserted into the database is valid, however, the problem is that the attached snippet checks and I also wanted to set the validation such as it should give the error message if the condition satisfies want to remove the blank spaces from left and right of the string.can any one help me to solve this problem. Guidelines will also be appreciated.

  2. #2
    Join Date
    Jan 2006
    Posts
    7,109

    Re: How to Replace blank fields with Javascript

    Using the following simple method you may replace the blank fields in javascript

    Code:
    string=string.replace(/(^\s+)(\s+$)/, "");
    There are also two more solutions – 1) don’t use innerHtml to get the contents of the span, use innerText, and 2) get the correct Javascript syntax for the replace.

  3. #3
    Join Date
    Dec 2008
    Posts
    183

    Re: How to Replace blank fields with Javascript

    Using the Replace() method you can easily replace the white space from a string with any character you want, or totally remove it. For that i used onkeyup=fun(); event in <input> tag.. calling function. The function is function fun() { var name=document.getElementById('name').value; document.getElementById('name').value=name.replace(/[^A-Za-z]/g,'');

    Then you do NOT need to check anything. Just replace the offending characters with a blank:
    else if( x[i].name=="description")
    {
    x[i].value = x[i].value.replace(/[^A-Za-z0-9 @\,\;\"\'-]/g,"");
    }

  4. #4
    Join Date
    Jan 2009
    Posts
    140

    Re: How to Replace blank fields with Javascript

    Use the following code for your problem.

    Code:
    <html>
    <head>
    <title>Remove spaces.</title>
    <script language="JavaScript">
    function fun() {
    var name=document.getElementById('name').value;
    document.getElementById('name').value=name.replace(/^(?:\s)*/g,'').replace(/(?:\s)*$/g,'');
    }
    </script>
    </head>
    <body>
    <form>
    <input type="Text" id="name" chelur="fun()">
    </form>
    </body>
    </html>

Similar Threads

  1. Some fields appers blank in Adobe form
    By Gimelstob in forum Windows Software
    Replies: 7
    Last Post: 29-06-2010, 02:02 AM
  2. Javascript to replace content of div
    By Kalyug in forum Software Development
    Replies: 7
    Last Post: 20-05-2010, 10:10 AM
  3. JavaScript - location.replace
    By Cordaro in forum Software Development
    Replies: 6
    Last Post: 13-05-2010, 11:55 PM
  4. javascript replace function not working
    By Cornnelius in forum Software Development
    Replies: 5
    Last Post: 13-05-2010, 12:12 AM
  5. Javascript to replace plus sign
    By Jagriti in forum Software Development
    Replies: 3
    Last Post: 22-06-2009, 08:30 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,236,544.43844 seconds with 17 queries