Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 18-08-2009
Member
 
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.
Reply With Quote
  #2  
Old 18-08-2009
Member
 
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.
Reply With Quote
  #3  
Old 18-08-2009
Member
 
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,"");
}
Reply With Quote
  #4  
Old 18-08-2009
Member
 
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>
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to Replace blank fields with Javascript"
Thread Thread Starter Forum Replies Last Post
Some fields appers blank in Adobe form Gimelstob Windows Software 7 29-06-2010 02:02 AM
Javascript to replace content of div Kalyug Software Development 7 20-05-2010 10:10 AM
JavaScript - location.replace Cordaro Software Development 6 13-05-2010 11:55 PM
javascript replace function not working Cornnelius Software Development 5 13-05-2010 12:12 AM
Javascript to replace plus sign Jagriti Software Development 3 22-06-2009 08:30 PM


All times are GMT +5.5. The time now is 01:21 AM.