Check if field contains certain characters

Hi,

Is it possible to check if a field contains certain characters, I thought I could use the following:

I'm checking for & , '

if(!/(\-|\&|\')$/i.test(fld.value)) {
alert("Your file contains invalid characters, please rename your file.");
fld.focus();
return false;
}

Thanks in advance.
sanjshah12Asked:
Who is Participating?
 
leakim971PluritechnicianCommented:
Instead use :

 if(/[&'\-,]/.test(fld.value)) {
0
 
leakim971PluritechnicianCommented:
if(/&|'/.test(fld.value)) {
   alert("Your file contains invalid characters, please rename your file.");
   fld.focus();
   return false;
}
return true;
0
 
sanjshah12Author Commented:
thanks leakim971,

just to check I need to ensure that &-,'/% are not contained in the filename - so I added:

      if(/&|'|-|,/.test(fld.value)) {

but don't think that I'm doing this correctly.


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.

 
sanjshah12Author Commented:
Thanks that works!
0
 
sanjshah12Author Commented:
thanks leakim971 for the links.
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.