471,851 Members | 1,042 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,851 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 2831
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

Post your reply

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

Similar topics

9 posts views Thread by lawrence | last post: by
4 posts views Thread by Wysiwyg | last post: by
2 posts views Thread by windandwaves | last post: by
3 posts views Thread by =?Utf-8?B?SGFycnkgVg==?= | last post: by
NeoPa
reply views Thread by NeoPa | last post: by
reply views Thread by YellowAndGreen | last post: by
aboka
reply views Thread by aboka | last post: by

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.