Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there an easy way to strip punctuation from a form object? 2

Status
Not open for further replies.

jewilson2

Technical User
Feb 7, 2002
71
0
0
US
I have a form with several text objects in which the user can freetype. I am converting them to uppercase with an onchange event as shown below. My question: is there a simple way to also strip out any special characters either onchange or onblur?

Code:
+ '<tr><td align=left nowrap><strong>First Name,MI</strong>'
+ '<td colspan=3><input type=text name=firstname value="' + CurrentDep.first_name
+  '" size=12 maxlength=12 onFocus=this.select() onChange="javascript:this.value=this.value.toUpperCase();">'
 
Code:
<script language="javascript">
function stripSpecial(str) {
   str.replace(/[^A-Z0-9]/g, "");
   [i][COLOR=grey]//this will replace all characters except A thru Z and 0 thru 9[/color][/i]
   [i][COLOR=grey]//if you want more characters to be excluded, add them between the brackets[/color][/i]
}
</script>

--and the call to the function--

onChange="javascript:this.value=this.value.toUpperCase();[b][COLOR=red]stripSpecial(this.value)[/color][/b]"

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
I'm not quite sure why, but this doesn't work.
 
Whoops, my bad. I left out the part that assigns the value. Make these corrections:
Code:
<script language="javascript">
function stripSpecial(str) {
   [b][COLOR=red]return([/color][/b]str.replace(/[^A-Z0-9]/g, "")[b][COLOR=red])[/color][/b];
   [i][COLOR=grey]//this will replace all characters except A thru Z and 0 thru 9
   //if you want more characters to be excluded, add them between the brackets[/color][/i]
}
</script>

--and the call to the function--

onChange="this.value = [b][COLOR=red]stripSpecial(this.value.toUpperCase())[/color][/b]"

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
Ok, this has me baffled. Still won't work

I tried your suggestion and it even stopped uppercasing the input. I stripped it down (forget the uppercase for now) and tried to make just the replace.str work...still no go.

Code:
function stripSpecial(str)
{
   return(str.replace(/[^A-Z0-9]/g, ""));
   //this will replace all characters except A thru Z and 0 thru 9
   //if you want more characters to be excluded, add them between the brackets
}

....and the call....

+ '<td colspan=3><input type=text name=firstname size=12 maxlength=12 onFocus=this.select() onChange="javascript:this.value=stripSpecial(this.value);">'

I certainly appreciate the help here.
 
<html>
<head><title>test</title></head>
<script language="JavaScript">

function stripSpecial(str)
{
return(str.replace(/[^A-Z0-9]/g, ""));
}

</script>
<body>
<form name="test">
<input type="text" id="firstname" name="firstname" value="" onKeyup="this.value = stripSpecial(this.value.toUpperCase())">
</form>
</body>
</html>
 
Still no go, but I think I know why. If I put ya'lls suggestions into a htm file of its own it works like a champ. Trying to use the same logic in this form I'm trying to customize doesn't work. I think it's because these values from the text boxes are assigned to objects further down in the code so that they can update the database. In other words, I'm trying to pass them as strings through the replace function and it doesn't understand what I'm trying to do. Here is the code further down where it's assigning the value from that text box to a database object....

Code:
function ProcessEmdepend(obj)
{
	DepForm = obj

	if (NonSpace(obj.firstname.value) == 0)
	{
	   	alert("First name is required")
	   	obj.firstname.focus()
	   	obj.firstname.select()
	   	return
	}

.
.
.
.
.
function Call_HR13(nowarning)
{
	var pObj = new AGSObject(authUser.prodline, "HR13.1")
	pObj.rtn = "MESSAGE"
	pObj.longNames = "ALL"
	pObj.tds = false
	pObj.debug = false

   	if (DepForm.fc.value == "A")
   	{
	  	pObj.event = "ADD"
	  	pObj.field = "FC=Add"
   	}
   	else
   	{
	  	pObj.event = "CHANGE"
	  	pObj.field = "FC=Change"
   	}

   	pObj.field += "&EMD-COMPANY=" + escape(parseInt(authUser.company,10),1)
	+ "&EMD-EMPLOYEE=" + escape(parseInt(authUser.employee,10),1)
	+ "&EMD-SEQ-NBR=" + escape(parseInt(DepForm.seqnbr.value,10),1)
	+ "&EMD-FIRST-NAME=" + escape(DepForm.firstname.value,1)

yadda yadda yadda

Again, thanks for the help on this folks. I'm working on how to pass the obj.value through that replace function, and still return it as a obj.value vs a string.

Clear as mud?
 
Finally got this to work. I had to assign variables to swap back and forth between the obj.value and the strings to get it to pass thru the function.

Code:
function replaceSpecial(obj)
{
        var str = obj.value
	var nstr = ""
	nstr = str.replace(/[^A-Z0-9 ]/gi, "");
	obj.value = nstr
        return obj.value
}

---and the call----

+ '<td colspan=3><input type=text name=firstname size=12 maxlength=12 onFocus=this.select() onChange="parent.replaceSpecial(this);parent.UpperCase(this)">'

Thanks a million for putting me on the right track on this.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top