Saturday, 15 December 2018

How to tell if a string contains a certain character in JavaScript?




I have a page with a textbox where a user is supposed to enter a 24 character (letters and numbers, case insensitive) registration code. I used maxlength to limit the user to entering 24 characters.



The registration codes are typically given as groups of characters separated by dashes, but I would like for the user to enter the codes without the dashes.



How can I write my JavaScript code without jQuery to check that a given string that the user inputs does not contain dashes, or better yet, only contains alphanumeric characters?


Answer



To find "hello" in your_string



if (your_string.indexOf('hello') > -1)
{

alert("hello found inside your_string");
}


For the alpha numeric you can use a regular expression:



http://www.regular-expressions.info/javascript.html



Alpha Numeric Regular Expression


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...