Wednesday, 13 November 2013

Remove Special characters and invalid characters from in Phonegap with Android and Javascript

Html Part:

<body>
<form>
<textarea id="code"></textarea>
<input type="button" value="OK" onclick="convertString()"/>
</form>
</body>

Script Part:

<script>
function convertString()
{
    var phrase = document.getElementById('code').value;
        alert(phrase);
         var maxLength = 100;

        var returnString = phrase.toLowerCase();
        //Convert Characters
        returnString = returnString.replace(/ö/g, 'o');
        returnString = returnString.replace(/ç/g, 'c');
        returnString = returnString.replace(/ş/g, 's');
        returnString = returnString.replace(/ı/g, 'i');
        returnString = returnString.replace(/ğ/g, 'g');
        returnString = returnString.replace(/ü/g, 'u'); 

        // if there are other invalid chars, convert them into blank spaces
        returnString = returnString.replace(/[^a-z0-9\s-]/g, "");
        // convert multiple spaces and hyphens into one space      
        returnString = returnString.replace(/[\s-]+/g, " ");
        // trims current string
        returnString = returnString.replace(/^\s+|\s+$/g,"");
        // cuts string (if too long)
        if(returnString.length > maxLength)
        returnString = returnString.substring(0,maxLength);
        // add hyphens
        returnString = returnString.replace(/\s/g, "-"); 
       
        alert(returnString);
}
</script>

No comments:

Post a Comment