Click to See Complete Forum and Search --> : help wanted. need custom script
I need what I'm told is a somewhat easy script.
I need a script to insert a charachter into the beginning of a form field AFTER someone clicks the submit button.
e.g. if some one types WIDGET into an <input type="text" field, when they hit the submit button it gets submitted as ^WIDGET
feel free to contact me with a bid.
thank you for your time
Khalid Ali
10-16-2003, 01:26 AM
Code snippet below will do what you are after
<script type="text/javascript">
<!--
function Process(){
var frm = document.getElementById("form1");
var len = frm.length;
var aChar = "^";
if(frm.t1.value==""){
alert("Text field is empty");
frm.t1.focus();
return false;
}
frm.t1.value = aChar+frm.t1.value;
return true;
}
//-->
</script>
</head>
<body class="body">
<form id="form1" action="" onsubmit="return Process();">
<input type="text" name="t1"/>
<input type="submit" value="process" />
</form>
</body>
Charles
10-16-2003, 05:50 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>
<style type="text/css">
<!--
label {display:block; margin:1em 0em}
input {display:block}
-->
</style>
<form action="" onsubmit="this.string.value = '^' + this.string.value">
<div>
<label>String<input type="text" name="string"></label>
<button type="Submit">Submit</button>
</div>
</form>
Mayby I"m close....... here's what I did
I added the following above the <head>
<script type="text/javascript">
<!--
function Process(){
var frm = document.getElementById("form1");
var len = frm.length;
var aChar = "^";
if(frm.t1.value==""){
alert("Text field is empty");
frm.t1.focus();
return false;
}
frm.t1.value = aChar+frm.t1.value;
return true;
}
//-->
</script>
Then I added [onsubmit="return Process();"] like so;
this was the original;
<input TYPE=TEXT value=" " name="custom5" size="5" maxlength="10"">
And I changed it to this;
<input TYPE=TEXT value=" " name="custom5" size="5" maxlength="10" onsubmit="return Process();">
I know, I didn't do something right....... if it helps, thre page I'm trying to add this to is;
http://www.rhtubs.com/store/insulate-test.htm
I'm trying to add this to ALL the text fields on the page.
Charles
10-16-2003, 01:03 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>
<style type="text/css">
<!--
label {display:block; margin:1em 0em}
input {display:block}
-->
</style>
<form action="" onsubmit="for (var i=0; i<this.elements.length; i++) {if (this.elements[i].type == 'text') this.elements[i].value = '^' + this.elements[i].value}">
<div>
<label>String 1<input type="text"></label>
<label>String 2<input type="text"></label>
<label>String 3<input type="text"></label>
<label>String 4<input type="text"></label>
<button type="Submit">Submit</button>
</div>
</form>
hmmm, Well I thank you Charles for your post and the trouble it took you. Unfortunatly, I don't have a clue what to do with it or how to implement it into my form.
Thank you though
stepheno
10-16-2003, 08:48 PM
The onsubmit event should be added to the form tag not the input tags. Also you will need to add the frm.t1.value = aChar+frm.t1.value; line of code for each input field in your form you want to add the ^ to. When you do replace the t1 with the name of each input tag you are adding the character to.
I just realized, my original post was missing something.....it should have read, "help wanted, WILL pay!"
I'm getting more and more lost on this.
Charles
10-16-2003, 09:33 PM
Originally posted by lets
hmmm, Well I thank you Charles for your post and the trouble it took you. Unfortunatly, I don't have a clue what to do with it or how to implement it into my form.
Thank you though <form action="" onsubmit="for (var i=0; i<this.elements.length; i++) {if (this.elements[i].type == 'text') this.elements[i].value = '^' + this.elements[i].value}">
Between the time that the submit button is hit and the form is actually submitted that script will step through all of the form elements. If the element is a text box then its value will be prepended with a "^". And unless I've misunderstood, that's what you are after.
Sir Charles, thank you for your patience and additional explanation...... IT WORKS!
I can't thank you enough..... Let me know if there's a way I repay you for your kindness.
Again, thank you so SO much