Jump to content

Remove Special Characters for Input Fields


Recommended Posts

So, I was testing this new HowTo Article I found.

 

It seems that it will not work for the Inline Insert and Inline Edit, so, I made one, if ever anyone needs it

 

INLINE INSERT

<script type="text/javascript">
document.addEventListener('input', function(event) {
var x = document.getElementById("InlineAddFIELDNAME").value;
document.getElementById("InlineAddFIELDNAME").value=x.replace(/[^a-zA-Z0-9 ]/g, "");
});
</script>

I set the event listener to input as the Inline Insert and Edit is not a form, this will, however, PREVENT special characters for being typed or pasted into the input

 

INLINE EDIT

 
<script type="text/javascript">

document.addEventListener("click", function(event){
if(event.srcElement.getAttribute("data-cb-name") == 'InlineEdit'){

document.addEventListener('input', function(e){

var x = document.querySelector('input[name="InlineEditFIELDNAME"]').value;
document.querySelector('input[name="InlineEditFIELDNAME"]').value=x.replace(/[^a-zA-Z0-9 ]/g, "");

});

}
});

</script> 
 
 
These both works for me, just reply if you encounter any issues.
 
Note: I don't know how to restrict just SOME special characters, but, you can search for any regexes that you can use instead of 'replace(/[^a-zA-Z0-9 ]/g, "")'
Link to comment
Share on other sites

  • 1 year later...

Just to add to this, if you want your input fields to NOT accept special character inputs right from the get-go (because what the article does is it accepts the special characters and get rids of them once the user hits submit), you can use the following script:

 

Paste the following on the Footer of the Configure Fields screen:
 
<script>

document.addEventListener('DataPageReady', function (event) {
$('#InsertRecordFIELDNAME').on('keypress', function (e) {
var ingnore_key_codes = [34, 39];
if ($.inArray(e.which, ingnore_key_codes) >= 0) {
e.preventDefault();
}
});
});

</script>

Where ingnore_key_codes contain the key codes of the characters you want to be prevented from being typed in. In the above example, 34 and 39 refers to single quote and double quote, respectively.

Make sure to also replace FIELDNAME in InsertRecordFIELDNAME with the name of the field you want to apply the script to.

 

You may refer to this link for a list of the codes for the characters. Simply add them to the ingnore_key_codes array

 

Link to comment
Share on other sites

  • 1 year later...

Thanks, @futurist,

I found your post very helpful, but was overwhelmed by the number of special characters I'd have to exclude.  Your suggestion led me to find another method that allowed me to just set a range of valid characters - in my case, non-capitalized letters and numbers.  Here's how I was able to do it:


 

<script>
// Get the text input
const input = document.getElementById('InsertRecordUniqueNm');

// Add keypress event listener
input.addEventListener('keypress', function(event){
    
    // Get the key code
    let keycode = event.which || event.keyCode;
    
    // Check if key pressed is a special character
    if(keycode < 48 || 
     (keycode > 57 && keycode < 97) || 
     keycode > 122
    ){
        // Restrict the special characters
        event.preventDefault();  
        return false;
    }
});  
</script>

I used some code I found in this post at programmer portal to get to the finish line.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...