function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Animesh DattaAnimesh Datta 

Replace Special Charaters with Null value in InputText

Hi,
I have a InputText field in Page <apex:inputText value="{!Coupon}"/>.
I need to make Special Characters to Null value while User typing special charaters in the free text box.

Kindly Help!

Regards,
Animesh
Bhanu MaheshBhanu Mahesh
Hi Animesh,

Use String method replaceAll() to remove the special characters

String str;
str = str.replaceAll('[^a-z^A-z^0-9]', '');

Check the below link for reference
https://success.salesforce.com/answers?id=90630000000hRTkAAM

Regards,
Bhanu Mahesh
Himanshu ParasharHimanshu Parashar
Hi Animesh,

If you want to check while user type then you need to take help of javascript
 
<apex:inputText value="{!Coupon}" onkeyup="return isValidDate(this);"/>
<script>
function isValidDate(f){ 
    var re =/^[\d\/\.-]+$/;
   if (!re.test(f.value)) { 
//    alert("called");
//if you need to add a value instead that can also be done here
return false;
   }
 }
</script>

Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant

P.S.  If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
 
 
Animesh DattaAnimesh Datta
Hi Himanshu,

could you please clarify what is  test in the if statement. "if (!re.test(f.value)) { "
Regards,
Animesh
Himanshu ParasharHimanshu Parashar
Hi Animesh,

Please find following code.
 
<script type="text/javascript">
        var specialKeys = new Array();
        specialKeys.push(8); //Backspace
        specialKeys.push(9); //Tab
        specialKeys.push(46); //Delete
        specialKeys.push(36); //Home
        specialKeys.push(35); //End
        specialKeys.push(37); //Left
        specialKeys.push(39); //Right
        function IsAlphaNumeric(e) {
            var keyCode = e.keyCode == 0 ? e.charCode : e.keyCode;
            var ret = ((keyCode >= 48 && keyCode <= 57) || (keyCode >= 65 && keyCode <= 90) || (keyCode >= 97 && keyCode <= 122) || (specialKeys.indexOf(e.keyCode) != -1 && e.charCode != e.keyCode));
            
            return ret;
        }
    </script>
 
<apex:inputText value="{!Coupon}" onkeypress="return IsAlphaNumeric(event);" ondrop="return false;" onpaste="return false;"/>

Thanks,
Himanshu
Animesh DattaAnimesh Datta
Hi Himanshu,

Thank you very much for the solution, but ondrop and onpaste does not work with <apex:inputText/>. 
Now, special character are not coming when typing from keyboard, but if I paste special charater, it is still appearing. Is there any solution on it?

Regards,
Animesh