PDA

View Full Version : Form Javascript Help


JGodsall
05-14-2003, 08:23 PM
I require a javascript that will do the following (as an example):

A user will type in their name (either as two words in one form text box or one word each in two boxes)

Their name will come up as an alert (or in another text box) when a submit button is pressed, with pre-determined words added to their name, anywhere in their name (before, after, and/or in between)(this must be easily editable by me for different purposes). The words added must also be able to link onto the existing words as well as be separate words.

This sounds like it would be quite easy if you knew how to add words to an existing form field, which unfortunately I don't.
All help would be appreciated.

Philip M
05-14-2003, 08:36 PM
In principle that is easy:-

example:-

alert ("Sorry, "+truename+"! You are not permitted to post an ad from\nthat email address without obtaining clearance in advance. ");


I am afraid that I do not understand what is meant by:-

"The words added must also be able to link onto the existing words as well as be separate words".

Could you give some specific examples of what you are trying to achieve?

e.g. Welcome John. Are you happy, John? John, what are you doing?

Is that what you require?

JGodsall
05-14-2003, 08:38 PM
Thanks a lot. Forget about that bit, I can do that myself. Sorry if it was a bit confusing.
Unfortunately I could really do with it in a whole script which does what I said, if anyone has the time to do that. I could try myself, but it probably wouldn't work.

Specific examples for the alert would be:
word "first name input" "second name input"
"first name input" word "second name input"
"first name input" "second name input" word

Or any two of these, or all at the same time. I hope you can see what I am trying to achieve. I expect you'd have to use two separate first name and second name input boxes to do this ("input1" "input2"), but that doesn't matter.

HairyTeeth
05-14-2003, 09:51 PM
try this:


<html>

<head>

<title>Untitled</title>

<script type="text/javascript" language="javascript">
<!--;

//global variables;
var preTxt="[pre-text] "
var midTxt=" [mid-text] "
var postTxt=" [post-text]"


//Function for one input;
function oneField(f,outputType){
var re=/^(\s*\D+)(\s+)(\D+\s*)$/
var theString = f.fullName.value;
if(!re.test(theString)){
alert("Please enter your first and last name\nseparated by a space")
setFocus(f.fullName);
return false
} else{
theString = theString.replace(re,preTxt + "$1" + midTxt + "$3" + postTxt)
switch(outputType){
case "alert":
alert(theString)
break
case "page":
document.frmOutput.txtOutput.value=theString
break
}
}
return true;
}


//Function for two inputs;
function twoFields(f,outputType){

var theString = preTxt + f.firstName.value + midTxt + f.lastName.value + postTxt;

if(f.firstName.value==""){
alert("Please enter your first name.");
setFocus(f.firstName)
return false;
}else if(f.lastName.value==""){
alert("Please enter your last name.");
setFocus(f.lastName)
return false;
}else{
switch(outputType){
case "alert":
alert(theString)
break
case "page":
document.frmOutput.txtOutput.value=theString
break
}

}
return true;
}

function setFocus(fld){
fld.focus();
fld.select();
return;
}
//-->
</script>

</head>

<body>

<form action="javascript: alert('Success')" method="post" onsubmit="return twoFields(this,'alert')">
First Name:
<br />
<input type="text" name="firstName" value="" />
<br />
Last Name:
<br />
<input type="text" name="lastName" value="" />
<br />
<input type="Submit" value="Submit"/>
</form>

<hr />


<form action="javascript: alert('Success')" method="post" onsubmit="return oneField(this,'page')">
Full Name (format: <em>First</em> [space]<em> Last</em>)
<br />
<input type="text" name="fullName" value="" />
<br />
<input type="submit" value="Submit" />
</form>

<form name="frmOutput">
Output:
<br />
<textarea cols="30" rows="5" name="txtOutput"></textarea>
</form>
</body>

</html>

-to have only preTxt, make the other two global variables empty strings (ie. midTxt="")
-to have an alert, pass as parameter in the call the word 'alert'
-to have the name written to a textarea, pass the word 'page'

Hope that helped.