Click to See Complete Forum and Search --> : How to Change Form Field Value onClick


kwilliams
06-07-2004, 02:31 PM
Hello,

I am working with a page that uses ASP & VBScript, and I'm trying to figure out how to change the form field value to nothing when a user clicks inside of the field using the onFocus method, and change it back to "(detail)" when they go away without an entry. I know how to do this in JavaScript, but I need to know how to do this with VBScript instead.

This is what my form field looks like with JavaScript:
<input type="textarea" name="detail" rows='4' cols='50' value="(detail)" onFocus="if (this.value==this.defaultValue) this.value='';" onBlur="if (this.value=='') this.value=this.defaultValue;">

And this is the current form field without any such addition:
<%Response.Write "<tr><td><textarea rows='4' cols='50' name='detail'>(detail)</textarea></td></tr>"%>

Any & all help is appreciated. Thanks.

lmf232s
06-07-2004, 03:03 PM
dont think you can do what you want with asp or vbscript.
With both of these your form has to be submited for an action to occur. but with javascript its client side so it works.

vbscript and asp would work but you would have to submit the form, which i dont think you want.

kwilliams
06-07-2004, 03:08 PM
Hi lmf,

I did find this article at http://authors.aspalliance.com/glenncook/vbscript.asp

about using VBScript validation, and I tried it out, but it didn't work for me. So is this article wrong? Thanks for your quick response.

lmf232s
06-07-2004, 11:03 PM
What are you trying to accomplish?
Form validation? For user input.

What your javascript do and the link you provided do, kind of do different things. Your javascript is able to fire client side with out the page being resubmit. The way the code is set up is that, if the textarea has focus then it changes its value and if it loses focus then its set to another. Not really validation. Because its javascrpt and client side code all its doing is changeing its value when you click in the textarea and when you leave.

But the link you provided does it server side and the page must be resubmited before any action can occur and the code is triggered with the click of a button. I will show you two examples, the vbscript and a asp version.

VBSCRIPT

<%option explicit%>
<HTML>
<HEAD>
<SCRIPT LANGUAGE="VBScript">
dim validation
dim header
header = aspalliance.com

Function MyForm_OnSubmit
validation = True
If Len(Document.MyForm.MyBox.Value) > 2 Then
MsgBox "You have entered too many characters! You need fewer characters before I will submit this form",8, Header
validation = False
End If
If (Document.MyForm.MyBox.Value) = "" Then
MsgBox "You have forgotten to fill in the input box! Why would you want to submit nothing in a one field form? C'mon, give me something to work with!",8, Header
validation = False
End If

If validation = True Then
MyForm_OnSubmit = True
Else
MyForm_OnSubmit = False
End If
End Function
</SCRIPT>
</HEAD>
<BODY>
<form METHOD="POST" name="MyForm">
<input TYPE="submit" VALUE="Submit Info!" name="submit">
<input type="text" name="MyBox" size="10">
</BODY>
</HTML>



ASP

<%option explicit%>

<%
dim hidsubmit, txtSearchString, errMsg

hidsubmit = Request.Form("hidsubmit")
txtSearchString = Request.Form("txtSearchString")
Select Case hidsubmit
Case "Submit"
if txtSearchString = "" then
'do something
txtSearchString = null
errMsg = "Invalid entry!"
else
'txtSearchString is = to something
Response.Write "The value you entered is " & txtSearchString
end if
Case else
end select
%>
<form name=form method=post>
<input type=hidden name=hidsubmit value="">
<table>
<tr>
<td><font color=red><%=errMsg%></font></td>
</tr>
<tr>
<td><input type=text name=txtSearchString value="<%=txtSearchString%>"></td>
</tr>
<tr>
<td><input type=submit name=cmdSubmit onclick="document.form.hidsubmit.value='Submit';document.form.submit();"></td>
</tr>
</table>


All this does is that on the click of the button it sets the hidden txt box named hidsubmit = to the value you set on the onclick event.
It then drops through a select case looking for a match. You can do what ever you want. I set up a simple error message that is displayed in red but you could do some javascript or even vbscript to create a msg box.

Hope this helps.

kwilliams
06-08-2004, 09:24 AM
It helps a lot. Since I'm wanting the validation to occur on the form page before submission, I'll stick with adding JavaScript to a VBScript page. Thanks for the clarification, as I am a newbie to VBScript and its limitations.

lmf232s
06-08-2004, 10:54 AM
ya if validaion is going to be done before the page is submitted then javascript is the way to go.

You have a couple of options, as the example you provided will keep changing the value as you give the textarea focus and then take focus away, so you are really not validating anything.

Heres an example using what you had posted originally

<input type="textarea" name="detail" rows='4' cols='50' value="(detail)" onFocus="if (this.value==this.defaultValue) this.value='';" onBlur="if (this.value=='') this.value=this.defaultValue;">

I would change this and do it one of two ways. You can validate after someone has entered some text or you can click a button which would do all the client side validation, and once the validation is complette you could use javascript to resubmit the form.

<script language=javascript>
function ValidateMe(){
if(document.form.detail == ""){
'then its null do something
}
else{
'it has a value its good and validation passes
'submit the page
form.action = "MyPage.asp";
form.submit();
}
}
</script>

<input type="textarea name=detail rows=4 cols=50 onchange="ValidateMe()">

This example is just for one text but you could do something like this to loop thourgh all the objects on your screen. This is considering you have stuff that needs to filled in or what ever. This is a good loop and you can use it many different ways.

function FormatMeBaby(){
var oForm = document.form;
var i, sType, sValue, sName, errName;
for (i = 0; i < oForm.elements.length; i++){
sType = oForm.elements[i].type;
sValue = oForm.elements[i].value;
sName = oForm.elements[i].name;
if (sType == "text"){
Temp = sName
if (Temp.match("txtPHandPer")){
//format %
if(isNaN(parseFloat(sValue))==false){
oForm.elements[i].value = formatNumber(oForm.elements[i].value, "%");
}

}
else{
if (Temp.match("txtP")){
if(Temp.match("txtPart")){
//do nothing, fild is part number, no need to format.
}
else{
if (isNaN(parseFloat(sValue))==false){
oForm.elements[i].value = formatNumber(oForm.elements[i].value, "$,##0.000");
}
}
}
}
}
}
}


This loop just shows that you can loop through every element on the form and check something.
Good luck