This page was last modified on June 23, 2008
Hello visitors
I thought to update this page as this page has had 9885 views so far as obvious in my statistics copied here
Top Posts Title: Javascript check to see if string starts with ’ste’ OR ‘Ste’, 11,191 views
so being the hottest item on my blog, it deserves more attention and work.
I know you came here looking for a way to do string processing in Javascript. For example you would like to search the text within a
<html>
<head>
<script language=javascript>
function CheckIt()
{
var element = document.getElementById(“theString”).value;
var subSection = element.substring(3,0);
var answer = false;if(subSection == “ste” )
answer = true;if(subSection == “Ste” )
answer = true;alert(answer);
}
</script>
</head>
<body><input type=text id=theString><input type=button value=”Check” onclick=CheckIt()>
</body>
</html>
Also, if you want to compare ‘ste’ in any case, you could just:
function CheckIt()
{
var element = document.getElementById(“theString” ).value;
var subSection = element.substring(3,0).toLowerCase();
var answer = (subSection == “ste”);alert(answer);
}
Nice blog. I like your design. How do you make a variable into a string tho?
As this page is very popular among my readers, so it was updated again today 21st May 2008.
vaticanism:
You can make a variable into a string in a very easy way. So here is a very good resource that will help you converting any javascipt variable into a string
http://www.jibbering.com/faq/faq_notes/type_convert.html#tcString
—
Regards
Fahd Murtaza
Why not use a regular expression to check for the start and allow both upper and lower case “S”, i.e. /^[Ss]te/. The ^ shows the start of the string, the [Ss] shows either upper or lower case S for the first letter, and the te show the next two letters.
The string.match() function will return the string or null.
Also, you should probably be sure that the string exists to avoid run time errors that may result in unpredictable performance.
function checkit()
{
var element=document.getElementById(”theString”);
if (element != null)
{ return (element.value.match(/^[Ss]te/) != null); }
else
{ return false; }
}
Pingback: Need help programming