bKashOST bKashOST - 16 days ago 5
Javascript Question

To detect special character in javascript

i want to identify special character like @, # in a input string using javascript but it's not working. Below is my javascript code.

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');

}

}


}


For input i am using below code.

<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>


Please help me to get this solution

Thanks

Answer

Try to make it this way:

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

Here is the full code:

    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>