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
pjordannaAsked:
Who is Participating?
 
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
 
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
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
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.