473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

End a for loop if value is ""

57
Hi all.

I have a small script which checks a text field on a form against values in a select box, then alert either 'Match' if there is a match or 'no match' if there is no match. However, I would like the script to end if the text field is blank.
The code is:

Expand|Select|Wrap|Line Numbers
  1. function compareID(){
  2.  
  3. var idlist = document.getElementById('1stDefinition').options;
  4. var idinput = document.getElementById('existingid');
  5. var match = false;
  6.  
  7. for (i=0; i<idlist.length; i++) {
  8.   if (idlist[i].value.toLowerCase() == idinput.value.toLowerCase()){ match = true;  }
  9. if (match) { alert("match"); } else {
  10.     alert("no match");
  11.     document.Enrolment.existingid.focus();}
  12.  
  13.  
  14. }
And the script os called on an onblur:

[HTML]<input type="text" name="Users_ID" size="10" maxlength="8" style="text-transform: uppercase" id="existingid" onblur=compareID();>[/HTML]

What seems to happen is, if the text field is clicked into and the left blank - the onblur says 'no match' and puts the cursor back in the text field - what it should do - if the text field is blank on blur then the script should just end.

Suggestions?????
Sep 11 '07 #1
4 1320
mrhoo
428 256MB
Don't start the loop if there is no input-

Expand|Select|Wrap|Line Numbers
  1. var idinput = document.getElementById('existingid');
  2. if(/^(\w)+$/.test(idinput.value)==false) return;
Sep 11 '07 #2
pbmods
5,821 Expert 4TB
Heya, Plumba.

Changed your thread title for you (I'm sure you meant a FOR loop, not an IF loop).

Have a look at the break keyword.
Sep 11 '07 #3
plumba
57
Heya, Plumba.

Changed your thread title for you (I'm sure you meant a FOR loop, not an IF loop).

Have a look at the break keyword.
Many thanks, suggestion worked a treat.

Cheers guys!!
Sep 12 '07 #4
pbmods
5,821 Expert 4TB
Heya, Plumba.

Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Sep 12 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: fartsniff | last post by:
I swear, these loops and for each's are going to kill me (if regex doesn't first ;) anyways, this is what i have: if ($_POST != "") { @$item_name1 = $_POST; @$item_number1 = $_POST; }
32
by: Toby Newman | last post by:
At the page: http://www.strath.ac.uk/IT/Docs/Ccourse/subsection3_8_3.html#SECTION0008300000000000000 or http://tinyurl.com/4ptzs the author warns: "The for loop is frequently used, usually...
6
by: John Pass | last post by:
What is the difference between a While and Do While/Loop repetition structure. If they is no difference (as it seems) why do both exist?
3
by: Patrick Sullivan | last post by:
In this for loop, IE skips over the animation function until the end of the loop and only aninmates the last phrase. Firefox does it right. Loop is right below, entire script is below that. This...
1
by: mark | last post by:
Forgive me if this seems like a stupid question but I need help... I'm trying to do a simple online form that emails me the results from a few fields. Here is the code: <form...
15
by: Steve | last post by:
I am having problems getting values out of an array. The array is set as a global array and values are pushed into it as they are read from a JSON file using a "for loop". When the "for loop" is...
9
by: Alexnb | last post by:
Okay, so lets say you have a list: funList = and you do: for x in funList: print x this will print 1-5
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.