By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,836 Members | 2,004 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,836 IT Pros & Developers. It's quick & easy.

Validate user entry against array for getURL

P: 3
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
Share this Question
Share on Google+
5 Replies


acoder
Expert Mod 15k+
P: 16,027
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
Expert 100+
P: 427
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

P: 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
Expert 100+
P: 427
@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

P: 3
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.