Reputation: 13
Hello I have a classified site and need a little help from javascript experts. When users post an ad in my site they tend to use ALL CAPS. like : "HELLO THIS IS MY POST TITLE" or some times special symbols like " * HELLO this is >>>>> a special ads special symbol || 999999 "
by using javascript how to make only the first letter of title in caps and rest in small and also exclude all special symbols after they post it (even with caps and symbols in the title)
Upvotes: 0
Views: 665
Reputation: 2671
var str = '567* hELLO 565 this is >>>>> a special ads special sy3mbol || 999999* fgdg'
str = str.toLowerCase().match(/[\w\d]+(?=[\W])|([\d\w]+)/g).join(' ')
str = str.replace(/\w/,function($1) {
return $1.toUpperCase()
})
Upvotes: 0
Reputation: 17720
Not sure why you would want to do that in Javascript rather than server-side, but here's a possibility:
var text = $('#title').val().toLowerCase().replace(/[^\w\s]/g,"").replace(/(^\s+|\s+$)/g,"").replace(/\s+/g," ");
$('#title').val(text.charAt(0).toUpperCase() + text.substr(1);
It only keeps letters and digits, makes sure there are no extra spaces, lowercases everything, uppercases the first letter.
Note that you may want to allow some special characters like " (for 32" for instance).
Upvotes: 1
Reputation: 420
To add on the above answer, use the replace function with regex. This should work:
string.replace([(\w|\d|\s]+, "");
Upvotes: 0
Reputation: 4669
For starters:
function capitaliseFirstLetter(string)
{
string.toLowerCase();
return string.charAt(0).toUpperCase() + string.slice(1);
}
For the rest, just do a quick google search on how to clean a JS string. Hint, you can write your own with a search/replace function. Look up about RegEx functions too.
Upvotes: 1