[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

CHECK FIRST 3 CHARACTERS OF TEXT STRING

Hi Experts,


I need to complete the code below so that if the first 3 characters of "formObj.medRefNo.value" are not "ABC" then the alert is called. Any ideas?


 var val = formObj.medRefNo.value
          if   (code needed here!)
               {
               alert('You have entered an invalid medical reference number please call our customer services department on XXXXXXXXXX');
               return false;
               }


Thanks,



PJORDANNA
0
pjordanna
Asked:
pjordanna
  • 3
  • 3
  • 2
1 Solution
 
BatalfCommented:
Try

 var val = formObj.medRefNo.value
 if(!val.match(/^ABC.*$/g)){
       alert('You have entered an invalid medical reference number please call our customer services department on XXXXXXXXXX');
       return false;
   }

0
 
Ramesh SrinivasTechnical ConsultantCommented:
var val = formObj.medRefNo.value
          if   (val.subtr(0,1)='A') ||  (val.subtr(1,1)='B') ||  (val.subtr(2,1)='C')
               {
               alert('You have entered an invalid medical reference number please call our customer services department on XXXXXXXXXX');
               return false;
               }
0
 
Ramesh SrinivasTechnical ConsultantCommented:
Sorry, i dunno if this is what you want.

var val = formObj.medRefNo.value
          if   (val.subtr(0,1)='A') && (val.subtr(1,1)='B') && (val.subtr(2,1)='C')
               {
               alert('You have entered an invalid medical reference number please call our customer services department on XXXXXXXXXX');
               return false;
               }
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Ramesh SrinivasTechnical ConsultantCommented:
Ahh, regular expression might be better way.
0
 
pjordannaAuthor Commented:
Batalf,

Oops! - sorry. I forgot. formObj.medRefNo.value needs to be able to be blank also

so either "" or first 3 characters must = "ABC"



Thanks


PJORDANNA
0
 
BatalfCommented:
Try

var val = formObj.medRefNo.value+"";
 if(!val.match(/^ABC.*$/g) && val!=""){
       alert('You have entered an invalid medical reference number please call our customer services department on XXXXXXXXXX');
       return false;
   }

0
 
pjordannaAuthor Commented:
Batalf,

Excellent work as usual.




PJORDANNA
0
 
BatalfCommented:
Glad I could help!

Just for your information, you can replace

if(!val.match(/^ABC.*$/g) && val!=""){

with

if(!val.match(/(^ABC.*$)|(^$)/g)){

if you like. i.e. everything in the same regexp.

But it doesn't make a difference on the end result.

Batalf
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now