View Full Version : for loop assistance needed
florida
03-25-2003, 03:11 PM
I have something that checks for blanks/spaces in my form and it works great but I was hoping to put it in a for loop to shorten it. Please advise how I can do this?
function stdCheck()
{
stdInfo = '';
if(/^\s*$/.test(document.stan.announce.value))
{
stdInfo += "\nAnnounce value";
}
if(/^\s*$/.test(document.stan.subject.value))
{
stdInfo += "\nSubject valuet";
}
if(/^\s*$/.test(document.stan.name.value))
{
stdInfo += "\nName value";
}
if (stdInfo != '')
{
{
stdInfo += "\n\nPLEASE ENTER THE ABOVE REQUIRED VALUES.";
alert(stdInfo);
return false;
}
}
my form part:
<FORM name="stan" action="myformadd" method="POST" enablecab="Yes" onSubmit="return stdCheck();">
joh6nn
03-25-2003, 05:12 PM
i think that using a for-loop in this particular case, would be just as long. for this, you have to set up some code to figure out what text value to put in the text boxes, and the easiest way to do that, would be based on the names of the text boxes. however, i think the code to that, would end up making the for-loop just as long as if you didn't use a loop.
florida
03-25-2003, 05:17 PM
Actually I am checking more than 3 fields but I condensed it for this example. I am checking about 40 fields and was hoping to shorten it in a for loop.
Weirdan
03-25-2003, 05:23 PM
try to use array
check_arr[0]={field_name,regexp,default_value}
check_arr[1]={field_name,regexp,default_value}
check_arr[2]={field_name,regexp,default_value}
and so on.
then loop through the form.elements array and check all fields.
cheesebagpipe
03-25-2003, 10:18 PM
<html>
<head>
<script type="text/javascript">
var testFields = new Array('announce' , 'subject' , 'name');
var msg = Object(); //store prompt strings, keyed by element names
msg['announce'] = '___your announcement';
msg['subject'] = '___your subject';
msg['name'] = '___your name';
function stdCheck(oForm) {
var el, focus_el, i = 0, stdInfo = 'PLEASE COMPLETE THESE REQUIRED FIELDS:\n\n';
while (el = oForm.elements[testFields[i++]]) //loop using testFields names
if (/^\s*$/.test(el.value)) { //check whitespace
stdInfo += '\t' + msg[el.name] + '\n'; //build prompt, using element name to extract string
if (!focus_el) focus_el = el; //assign element reference, first time only (to focus topmost field)
}
if (focus_el) {
alert(stdInfo);
focus_el.focus();
focus_el.select();
return false;
}
return true;
}
</script>
</head>
<body>
<FORM name="stan" action="myformadd" method="POST" enablecab="Yes" onsubmit="return stdCheck(this)">
<input type="text" name="announce" />___your announcement<br />
<input type="text" name="subject" />___your subject<br />
<input type="text" name="name" />___your name<br><br />
<input type="submit" />
</form>
</body>
</html>
Pass that Form object!
florida
03-28-2003, 02:42 PM
Thanks (again) for solving my problem.
It works great with <form> but doesnt work with <cfform>(Cold Fusion form). I assume Javascript works better (in this case) in <form> instead of <cfform>?
Also what is the "Object()" in this line? Is it a Javascript funtion?
var msg = Object();
Thanks for all your help!
cheesebagpipe
03-28-2003, 10:10 PM
CF-impaired here, so, who knows? Found this (http://www.macromedia.com/support/coldfusion/ts/documents/tn18157.htm) which might help; hopefully someone here who knows ColdFusion will chime in.
var msg = Object(); ==> creates a new generic JS object.
It's a (constructor) function; these are usually called with the object instantiation operator (new) but, presumably due to its representing the core JS object, the new can be skipped in this case, as long as you assign some properties immediately to 'cement' the new object. Use var msg = new Object(); if you like, or even var msg = new Object; - calls to host constructors (Array(), Object(), String(), Number(), Boolean()) don't require the parentheses, presumably because they represent JS classes. You could use an array object as well - these house both ordered lists (myArray[0]) and hashtables (myArray['myProperty']) under the same roof - but it's a good idea to generate the specific type of object you need, and nothing more.
http://www.javascriptkit.com/javatutors/oopjs.shtml
florida
04-01-2003, 01:24 AM
Thanks again for all your assistance!
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.