469,579 Members | 1,830 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,579 developers. It's quick & easy.

How to check for special characters in js?

omerbutt
638 512MB
hi i have the following code for javascript but the problem is that it detects the "$" and "s" as same and whenever it finds the "s" letter in the word it says that it has special characters
Expand|Select|Wrap|Line Numbers
  1. checkSpecialChars(fileOnlyName);
  2. function checkSpecialChars(fileOnlyName){
  3.     //var iChars = "!@#$%^&*()+=-[]\\\';,/{}|\":<>?~"; 
  4.     fileOnlyName=String(fileOnlyName);
  5.     var iChars = "!@#$%^&*+-[]\\\';,/{}|\":<>?~"; 
  6.     var charAr    =    iChars.split("");
  7.     for(var i=0; i<charAr.length; i++){
  8.         if(fileOnlyName.search(charAr[i])!= -1){
  9.             return false;
  10.         }
  11.     }
  12.  
  13. return false
  14. }
  15.  
Oct 27 '10 #1

✓ answered by omerbutt

I DID IT
Expand|Select|Wrap|Line Numbers
  1. var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
  2.         var is_ff = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
  3.         var is_ie = navigator.userAgent.toLowerCase().indexOf('ie') > -1;
  4.  
  5.         if(is_ff){
  6.             ext = fname.substr(fname.lastIndexOf('.'));
  7.             fileOnlyName=fname.split(".");
  8.         }else if(is_ie){
  9.             namearray=fname.split("\\");
  10.             fileOnlyName=namearray[namearray.length-1].split(".");
  11.             ext = namearray[namearray.length-1].substr(namearray[namearray.length-1].lastIndexOf('.'));
  12.         }else {
  13.             namearray=fname.split("\\");
  14.             fileOnlyName=namearray[namearray.length-1].split(".");
  15.             ext = namearray[namearray.length-1].substr(namearray[namearray.length-1].lastIndexOf('.'));
  16.         }
  17.  
  18.         //alert(fileOnlyName[0]);
  19.  
  20.     if ((/[^a-z0-9\.\-\_\ ]/gi).test(fileOnlyName[0])) {
  21.         alert (fileOnlyName[0] + " contains invalid character(s)");
  22.         return false;
  23.     }
  24.  

8 9276
omerbutt
638 512MB
done
changed it to this
Expand|Select|Wrap|Line Numbers
  1. function checkSpecialChars(fileOnlyName){
  2. var filename = fileOnlyName;
  3.     if (/[^a-z0-9\.]/gi.test(filename)) {  // anything but a-zA-Z0-9 and dot
  4.         showMesage (filename + " contains invalid character(s)");  
  5.         return false;
  6.     }else{
  7.         return true;
  8.     }
  9. }
  10.  
Oct 27 '10 #2
Dormilich
8,652 Expert Mod 8TB
why not using the direct approach?
Expand|Select|Wrap|Line Numbers
  1. if (fileOnlyName.indexOf(iChars.charAt(i) !== -1) {
[edit]
you don’t need to escape . in a character class.
[/edit]
Oct 27 '10 #3
omerbutt
638 512MB
hey none of it worked , mine had issues in chrome and IE 8 it keeps on detecting a valid file name as an invalid file name
.your method i used before too but that didnt worked for me while googling i came across every other example with this particular line as you stated , currently em nowhere
:(
regards
Omer Aslam
Oct 27 '10 #4
Dormilich
8,652 Expert Mod 8TB
works for me, though.
Expand|Select|Wrap|Line Numbers
  1. function checkSpecialChars(fileOnlyName)
  2. {
  3.     if ((/[^a-z0-9\.]/gi).test(fileOnlyName)) {
  4.         alert (fileOnlyName + " contains invalid character(s)");
  5.         return false;
  6.     }
  7.     return true;
  8. }
Oct 27 '10 #5
omerbutt
638 512MB
hmm it wasnt actually this function i am trying to get the file name from file input tag that gives file name in the FF but in IE and Chrome it shows path now i didnt knew that so i was having the same check for chrome safari and FF , and separate for IE bucause i didnt knew that chrome also returns the file path instead of only file name, AND that is why it was saying it an invalid file name because the name sent to the function had the complete path istead of only file name now i need to detect the CHROME browser via javascript any idea how ?
regards,
Omer Aslma
Oct 27 '10 #6
Dormilich
8,652 Expert Mod 8TB
you could allow / and \ or simply extract the filename from the path …
Oct 27 '10 #7
omerbutt
638 512MB
I DID IT
Expand|Select|Wrap|Line Numbers
  1. var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
  2.         var is_ff = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
  3.         var is_ie = navigator.userAgent.toLowerCase().indexOf('ie') > -1;
  4.  
  5.         if(is_ff){
  6.             ext = fname.substr(fname.lastIndexOf('.'));
  7.             fileOnlyName=fname.split(".");
  8.         }else if(is_ie){
  9.             namearray=fname.split("\\");
  10.             fileOnlyName=namearray[namearray.length-1].split(".");
  11.             ext = namearray[namearray.length-1].substr(namearray[namearray.length-1].lastIndexOf('.'));
  12.         }else {
  13.             namearray=fname.split("\\");
  14.             fileOnlyName=namearray[namearray.length-1].split(".");
  15.             ext = namearray[namearray.length-1].substr(namearray[namearray.length-1].lastIndexOf('.'));
  16.         }
  17.  
  18.         //alert(fileOnlyName[0]);
  19.  
  20.     if ((/[^a-z0-9\.\-\_\ ]/gi).test(fileOnlyName[0])) {
  21.         alert (fileOnlyName[0] + " contains invalid character(s)");
  22.         return false;
  23.     }
  24.  
Oct 27 '10 #8
Dormilich
8,652 Expert Mod 8TB
and now it’s time to optimise the code … *gg*
Oct 27 '10 #9

Post your reply

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

Similar topics

3 posts views Thread by Barry Olly | last post: by
9 posts views Thread by Larry | last post: by
7 posts views Thread by Trac Bannon | last post: by
3 posts views Thread by orgelizer via AccessMonster.com | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.