Note that there are some explanatory texts on larger screens.

plurals
  1. POsearch() function of javascript does not behave properly
    primarykey
    data
    text
    <p>Don't know why search() function returns 0 for any input with SPECIAL CHARACTER, i wanted to find position of 1st occurance of special character. When i am hardcoaing the value for search() method it is working fine, but when i am taking value from text box it is not working properly.</p> <p>Following is my HTML code:</p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt; &lt;script type="text/javascript" src="jquery-1.6.4.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="test.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;input type="text" id="txt" onkeyup="return checkLength();"/&gt; &lt;input type="button" id="btn" value="Verify" onclick="getValue()"/&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Following is the script where I have implemented the use of search() of javascript, but don't know why i am getting 0 value for any input. Actually i wanted to find the postion of 1st special character occurance.</p> <pre><code>$(document).ready(function() { $('#btn').attr('disabled',true); $("#txt").bind({ paste : function(){ $('#btn').attr('disabled',false); checkLength(); }, cut : function(){ checkLength(); } }); }); function checkLength(){ var txtLength = $("#txt").val().length; var banTxt = document.getElementById("txt").value; if (txtLength != 0) { if(isAlphaNumeric(document.getElementById("txt").value)) { $('#btn').attr('disabled',false); } else { var str=banTxt; //Here I am using search() to find position of Special Character. var n=banTxt.search(/[^a-zA-Z ]/g); alert("position of special char is: " + n); var preTxt = banTxt.substring(0,(txtLength - 1)); var preTxtLength = preTxt.length; alert("Special characters are not allowed!"); if(preTxtLength == 0){ $('#btn').attr('disabled',true); document.getElementById("txt").value = ""; }else if(preTxtLength != 0){ document.getElementById("txt").value = preTxt; $('#btn').attr('disabled',false); } } } else { $('#btn').attr('disabled',true); } } function isAlphaNumeric(inputString) { return inputString.match(/^[0-9A-Za-z]+$/); } function getValue(){ var txtValue = document.getElementById("txt").value; alert("Value submitted is: " + txtValue); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload