...

View Full Version : Resolved Deleting string from input field



alphamale
10-08-2012, 04:55 PM
I have an input field that ends up having a lot of automated <pre></pre> tags.

Can I delete all of these from the input fields content on page load? and then can i wrap the fields contents in these tags on form submission?

basically so the user never has to see them.

Thanks,
Michael

WolfShade
10-08-2012, 05:18 PM
RegEx replace. UNTESTED.

string.replace(/\<(\/)*pre\>/g,"") when the page loads (or can be done server-side.. probably easier)

On form submit, again, server-side would be best, but you can use JavaScript to add the tags around the value.

inputID.value = "<pre>" + inputID.value + "</pre>"

alphamale
10-08-2012, 05:34 PM
RegEx replace. UNTESTED.

string.replace(/\<(\/)*pre\>/g,"") when the page loads (or can be done server-side.. probably easier)

On form submit, again, server-side would be best, but you can use JavaScript to add the tags around the value.

inputID.value = "<pre>" + inputID.value + "</pre>"

string.replace(/\<(\/)*pre\>/g,"")

I dont get this, what are all the special characters for? Does it just remove all <pre> and </pre> tags?

and for: inputID.value = "<pre>" + inputID.value + "</pre>"

could I use $newText = "<pre>"".$oldText."</pre>";
?

Thanks!

xelawho
10-08-2012, 05:35 PM
mmm... but will the user be able to edit the input fields once the page has loaded, and are there more than once set of tags per input field?

alphamale
10-08-2012, 05:45 PM
mmm... but will the user be able to edit the input fields once the page has loaded, and are there more than once set of tags per input field?

Heres an example of how bad the auto-tagging is getting...


<pre><pre><pre><pre><pre><pre><pre><pre>Testing description field</pre></pre></pre></pre></pre></pre></pre></pre>

This is a field for the user to edit (a profile update form).

I need to remove all <pre> and </pre> tags from that field on page load. Then rewrap the submission in <pre></pre> I know how to rewrap but not how to remove :/

Thanks

WolfShade
10-08-2012, 05:46 PM
string.replace(/\<(\/)*pre\>/g,"")

I dont get this, what are all the special characters for? Does it just remove all <pre> and </pre> tags?

and for: inputID.value = "<pre>" + inputID.value + "</pre>"

could I use $newText = "<pre>"".$oldText."</pre>";
?

Thanks!

I haven't tested it (not enough time, at work), but the extra characters (I assume you mean the backslashes) are for escaping, making the <, /, and > characters "literals" instead of programming commands.

It _should_ just remove the "<pre>" and "</pre>" tags from the string.

As far as $newText and whatnot, that looks like PHP, not JavaScript.

xelawho
10-08-2012, 05:55 PM
here's how WolfShade's regex would work on page load:



<body>
<input id="txt" value="<pre><pre><pre><pre><pre><pre><pre><pre>Testing description field</pre></pre></pre></pre></pre></pre></pre></pre>"/>
<script type="text/javascript">
document.getElementById("txt").value=document.getElementById("txt").value.replace(/\<(\/)*pre\>/g,"")
</script>
</body>


if you have the rewrap bit sorted, you should be fine.

alphamale
10-08-2012, 06:07 PM
here's how WolfShade's regex would work on page load:



<body>
<input id="txt" value="<pre><pre><pre><pre><pre><pre><pre><pre>Testing description field</pre></pre></pre></pre></pre></pre></pre></pre>"/>
<script type="text/javascript">
document.getElementById("txt").value=document.getElementById("txt").value.replace(/\<(\/)*pre\>/g,"")
</script>
</body>


if you have the rewrap bit sorted, you should be fine.

Okay thanks again.

Just to confirm that this would be okay?



<body>
<textarea id="description"> <?php $value['description'] ?> </textarea>
<script type="text/javascript">
document.getElementById("description").value=document.getElementById("description").value.replace(/\<(\/)*pre\>/g,"")
</script>
</body>

WolfShade
10-08-2012, 06:11 PM
That should do it. I can never remember if * or ? is "zero or more of the preceeding character", but it's one or the other.

alphamale
10-08-2012, 06:35 PM
Worked like a charm! Thanks :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum