2016-11-23 95 views
0

我想使用JavaScript在輸入字符串中識別特殊字符,如@,#但它不起作用。以下是我的JavaScript代碼。要檢測javascript中的特殊字符

function check_cr_title(selected_cr_str) 
    { 
     if(selected_cr_str != '') 
     { 
      if(selected_cr_str.match(^[@#]+$)) 

      { 
       jQuery("#check_valid_cr_span").html(selected_cr_str + ' Contains Special character.'); 
       jQuery("#check_valid_cr_span").css('color','red'); 
       jQuery("#check_valid_cr_span").css('display','block'); 
       jQuery("#cr_title").val(''); 


      } 
      else 
      { 
       jQuery("#check_valid_cr_span").html(''); 
       jQuery("#check_valid_cr_span").css('display','none'); 

      } 

     } 


    } 

對於我使用的輸入代碼如下。

<div class="row"> 
    <div class="column1 columnheader ">CR Title</div> 
    <div class="column2 column-input width100"><input name="cr_tile" type="text" value="" onblur="check_cr_title(this.value)" required></div> 
    <span id="check_valid_cr_span" style="display:none;width:1000px;"></span> 
    </div> 

請幫我解決這個問題。

+1

你想匹配整個字符串只包含特殊字符嗎?或者只是字符串是否包含特殊字符?如果後者那麼你應該放棄開始和結束標記 –

+0

我正在接受一個字符串的輸入。我想檢查該字符串中是否存在特殊字符。 – bKashOST

+0

[jQuery驗證方法檢查特殊字符]的可能重複(http://stackoverflow.com/questions/14949257/jquery-validate-method-checking-for-special-characters) – Shekhu

回答

0

儘量做到這樣:

if(selected_cr_str.match(/[@#]/g)) 

下面是完整的代碼:

function check_cr_title(selected_cr_str) 
 
    { 
 
     if(selected_cr_str != '') 
 
     { 
 
      if(selected_cr_str.match(/[@#]/g)) 
 

 
      { 
 
       jQuery("#check_valid_cr_span").html(selected_cr_str + ' Contains Special character.'); 
 
       jQuery("#check_valid_cr_span").css('color','red'); 
 
       jQuery("#check_valid_cr_span").css('display','block'); 
 
       jQuery("#cr_title").val(''); 
 

 

 
      } 
 
      else 
 
      { 
 
       jQuery("#check_valid_cr_span").html(''); 
 
       jQuery("#check_valid_cr_span").css('display','none'); 
 

 
      } 
 

 
     } 
 

 

 
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="row"> 
 
    <div class="column1 columnheader ">CR Title</div> 
 
    <div class="column2 column-input width100"><input name="cr_tile" type="text" value="" onblur="check_cr_title(this.value)" required></div> 
 
    <span id="check_valid_cr_span" style="display:none;width:1000px;"></span> 
 
    </div>

+0

它的工作如果@和#是在如果這些字符位於字符串的中間,則開始但不工作。 – bKashOST

0

unction check_cr_title(selected_cr_str) 
 
    { 
 
     if(selected_cr_str != '') 
 
     { 
 
      if(selected_cr_str.match(('\^[@# ]+$\'))) 
 

 
      { 
 
       jQuery("#check_valid_cr_span").html(selected_cr_str + ' Contains Special character.'); 
 
       jQuery("#check_valid_cr_span").css('color','red'); 
 
       jQuery("#check_valid_cr_span").css('display','block'); 
 
       jQuery("#cr_title").val(''); 
 

 

 
      } 
 
      else 
 
      { 
 
    jQuery("#check_valid_cr_span").html('');    jQuery("#check_valid_cr_span").css('display','none'); 
 

 
      } 
 

 
     } 
 

 

 
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="row"> 
 
    <div class="column1 columnheader ">CR Title</div> 
 
    <div class="column2 column-input width100"><input name="cr_tile" type="text" value="" onblur="check_cr_title(this.value)" required></div> 
 
    <span id="check_valid_cr_span" style="display:none;width:1000px;"></span> 
 
    </div>