Click to See Complete Forum and Search --> : eval to determine text object's name
BananaQuaalude
10-14-2003, 05:35 PM
I've tried several time to use the eval function for this task, and can never get it to work. I want to use the following function for every textbox on my form:
function noBlank(item) {
alert("Hours field may not be blank. To delete hours, insert a zero")
eval("document.frmData." + item.name + ".focus()");
}
The event tied to the <text> object in HTML:
onBlur="noBlank(this)"
I get an "Expected ; " error. Where is it looking for that?
Khalid Ali
10-14-2003, 07:07 PM
it seems to work..how about showing us the full code(html+js)
BananaQuaalude
10-14-2003, 10:10 PM
The page is pretty lengthy, but here are the affected parts:
<script language="JavaScript">
function noBlank(item) {
alert("Hours field may not be blank. To delete hours, insert a zero")
//eval("document.frmData." + item.name + ".focus();");
}
</script>
Here's the HTML:
<form name="frmData">
...other code here
<td><center><input type="text" name="Upd-<%=RowData & "-" & RS("EmpProjAssignId") & "-" & intFirstMonth%>" value="<%=intMonth%>" size="2" onBlur="noBlank(this)" onChange="ListTextChanges(this, <%=RowData%>)"><%
...other code here
The other function, called in the onChange event, works fine. But I still get the "Expected ;" error when calling noBlank. I've tried putting ';' everywhere feasible, to no avail.
?
--I just realized I haven't even added the code to test to see if the textbox is actually blank or not. I haven't gotten that far yet.
Charles
10-15-2003, 06:51 AM
There is almost never any reason to use the "eval()" function in JavaScript. If you are using it then you are likely to be doing something wrong.
<!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>
<script type="text/javascript">
<!--
function noBlank (element) {if (!/\S/.test(element.value)) {alert(element.parentNode.firstChild.data + ' field may not be blank. To delete hours, insert a zero.'); element.focus()}}
// -->
</script>
<form action="">
<div>
<label>Date<input onblur="noBlank(this)" type="text"></label>
<button type="submit">Submit</button>
</div>
</form>
BananaQuaalude
10-15-2003, 09:13 AM
Awesome - that works.
But could you break down this code:
if (!/\S/.test(element.value))
I think that's a regular expression saying if there is no number entered in the element's value, the statement is true. Is 'S' a digit?
Thanks!
Charles
10-15-2003, 11:49 AM
/\S/.test(element.value)
returns true if "element.value" contains any non-white space characters while
!/\S/.test(element.value)
negates that. it returns true if "element.value" is empty or contains only white space. See http://devedge.netscape.com/library/manuals/2000/javascript/1.3/guide/regexp.html.
BananaQuaalude
10-15-2003, 11:52 AM
Awesome - I'll check out that site.
Thanks!