473,397 Members | 2,077 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,397 software developers and data experts.

Validate user entry against array for getURL

This is the item selection piece for a Sharepoint knowledge base. If you type a valid plan code you get info, but if you type garbage you get a 404 file not found. I want to check user input against an array of allowable values (eg. A33, B33, A80, B22, etc...) .

true = go to the topic you put in; false = go to a form similar to the original but alerting you to invalid entry.

Script:
Expand|Select|Wrap|Line Numbers
  1. <SCRIPT type=text/javascript>
  2.  
  3. /* 
  4. Navigate to  specific plan code web page
  5. */
  6.  
  7.  
  8. function getURL(val){
  9. base = 'http://dhpulse.hosp.dhha.org/EducationTraining/DH_Revenue_Ed/DH_Rev_Ed_Resources/Ins%20Gadget/PlanCards/';
  10. location = base + val + '.htm';
  11. return false;
  12. }
  13.  
  14. </SCRIPT>


Form that collects input:

Expand|Select|Wrap|Line Numbers
  1. <FORM id=form2 name=form2 onsubmit="return getURL(this.url.value)" action="" 
  2. method=post>Enter a Plan Code<BR>
  3. <LABEL><INPUT size=3 name=url> </LABEL>
  4. <LABEL><INPUT type=submit value=GO name=Submit> </LABEL>
  5. </FORM>
Thanks for any help,
Jan 29 '09 #1
5 2960
acoder
16,027 Expert Mod 8TB
Declare an array:
Expand|Select|Wrap|Line Numbers
  1. var array = ["A33",...];
then loop over it to compare with the url (passed value). If it doesn't match, return false.
Jan 30 '09 #2
rnd me
427 Expert 256MB
you can avoid the loop by using an object to specify ok values.
this also avoids multiple comparisons, so it should be faster in theory, though i doubt for something small like this it matters much.

still, it's a handy pattern that's fast and avoids more complicated branching and comparing.

add any others in the same format (xx:1,) to customize...

Expand|Select|Wrap|Line Numbers
  1. function getURL(val){
  2.   if( ! ({ A33:1, B33:1, A80:1, B22:1 })[ val ]  ){ 
  3.     alert("not a valid val");
  4.     return;
  5.   }
  6. base = 'http://dhpulse.hosp.dhha.org/EducationTraining/DH_Revenue_Ed/DH_Rev_Ed_Resources/Ins%20Gadget/PlanCards/';
  7. location = base + val + '.htm';
  8. return false;
  9. }
  10.  
  11.  
  12. A33, B33, A80, B22
  13.  
  14.  
Jan 31 '09 #3
Thanks for both! I'm learning, so its very good to see both approaches. I see now that to polish it off, I'll either need to check for A33, a33, B33, b33, etc... or convert the case of the user input.
Jan 31 '09 #4
rnd me
427 Expert 256MB
@cstansbu
if that's the case, simply do it once in the object value matcher:

Expand|Select|Wrap|Line Numbers
  1.  
  2. function getURL(val){
  3.    if( ! ({ A33:1, B33:1, A80:1, B22:1 })[ String(val).toUpperCase() ]  ){ 
  4.      alert("not a valid val");
  5.      return;
  6.    }
  7.  base = 'http://dhpulse.hosp.dhha.org/EducationTraining/DH_Revenue_Ed/DH_Rev_Ed_Resources/Ins%20Gadget/PlanCards/';
  8.  location = base + val + '.htm';
  9.  return false;
  10.  }
  11.  
  12.  
notice how a string is created for safety, then the string is converted to uppercase for the comparison.

now you only need to code upper case versions.
Jan 31 '09 #5
Perfect. I was trying to put the toUppercase before the If
Jan 31 '09 #6

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

Similar topics

9
by: lawrence | last post by:
I'm running this page: http://www.krubner.com/rss/page938.xml through this validator: http://rss.scripting.com/?url=http%3A%2F%2Fwww.krubner.com%2Frss%2Fpage938.xml
1
by: aevans1108 | last post by:
Greetings All If this is the wrong place to post this question, please give me a push in the right direction. Thanks. I know there has to be a simpler way to do this, but this is as simple a...
6
by: LesleyW | last post by:
Hi Apologies if this is a really dumb question, but being new to XML and Schemas, I wonder if giving the namespace for eg xsd or xsi as a website address means that the user has to be online...
7
by: Ali-R | last post by:
Hi all, I am getting a CSV file like this from our client: "C1","2","12344","Mr","John","Chan","05/07/1976"......... I need to validate **each filed value** against a set of rules ,for...
4
by: Wysiwyg | last post by:
I need to validate a form to ensure that all of the fields add up correctly. I can't do this while the user is entering data since validation needs to be done after the entry is completed. What's...
2
by: windandwaves | last post by:
Hi Folk, I use AJAX to load some XML. When I get back to XML, I want to get a piece of html that is within <info>... lots of html .... </info> I want to use: ...
1
by: anilareddy | last post by:
I have an application like this. I need to validate the start time in 2nd row against the start row in first row. I mean The start time value entered in the second row must not b the value entered in...
3
by: =?Utf-8?B?SGFycnkgVg==?= | last post by:
Is it possible to validate a 2-character substring against a list of 13 2-character codes without a select statement to a table or parsing an array? Something like ... myChars in ("aa", "bb",...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.