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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

function(this)

Status
Not open for further replies.

ptichka

Programmer
May 28, 2002
19
US
Hello everybody,

I am pretty new at this so I keep getting an error that I can't figure out.

This is what I have:

function ValidateForm(field){
alert(field);
var dt=document.frmSample.field.value
if (isDate(dt)==false){
dt.focus()
return false
}
return true
}

</script>

<form name=&quot;frmSample&quot; method=&quot;post&quot; action=&quot;&quot;>
<p>Enter a Date <font color=&quot;#CC0000&quot;><b>(mm/dd/yyyy)</b></font>
:
<input type=&quot;text&quot; name=&quot;txtDate&quot; maxlength=&quot;10&quot; size=&quot;15&quot; onblur=&quot;ValidateForm(this)&quot;>
</p>
<p>
<input type=&quot;submit&quot; name=&quot;Submit&quot; value=&quot;Submit&quot;>
</p>
</form>


When thge function is called, in the alert I get a result that said [object]

And I also get a &quot;document.frmSample.field.value is null or not an object&quot;

Why am I keep getting this error??? Help
 
Your variable declaration and setting should be either:

var dt=field.value

OR

var dt=document.frmSample.txtDate.value

You mixed the two methods and that is why you are getting the error.
 
using the word this in the onblur method calls your javascript function (ValidateForm) and passes a reference to the calling object (in this case the txtDate text field).

So as XgrinderX said, you can just refer to it directly using the parameter you have called field.
Code:
  alert(&quot;field.name : &quot; + field.name + &quot;, field.value : &quot; + field.value);
  var dt=field.value;

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
XgrinderX and clarkin,

Thank you both for helping me.
I 've got the part that I couldn't to work now. But I also wanted to put the focus back on the field that the function is called from (I call the function onChange instead of onBlur), and I get an error as well.

I tired this:

function ValidateForm(field){
var dt=field.value
if (isDate(dt)==false){
alert(&quot;field.name : &quot; + field.name + &quot;, field.value : &quot; + field.value);
document.frmSample.field.name.focus()
return false
}
return true
}

And I get an error that sais:
document.frmSample.field.name is null or not an object

I thought the field.name should have gotten the name of the text field in there.
 
quick dirty answer: field.focus();

reason you can't see it: you don't understand the object model.

:(

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top