How to validate special character using javascript

How to validate Special Character in javascript?

File name : index.php

<input type="text" class="form-control" name="address" id="address" value="<?php echo set_value('address'); ?>" placeholder=" Enter Address"
onkeypress="return IsAlphaNumeric(event);" ondrop="return false;" onpaste="return false;"
/>
<span id="error" style="color: Red; display: none">* Special Characters not allowed.</span>
<span class="required" id="address_errr" aria-required="true"></span>
<span class="error"><?php echo form_error('address'); ?></span>

File name : index.php

<script type="text/javascript">
// special characters not allowed.
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 &&charCode != e.keyCode));
document.getElementById("error").style.display = ret ? "none" : "inline";
return ret;
*/

var k;
document.all ? k = e.keyCode : k = e.which;
var ret = ((k > 64 && k < 91) || (k > 96 && k < 123) || k == 8 || k == 32 || (k >= 48 && k <= 57));
document.getElementById("error").style.display = ret ? "none" : "inline";
return ret;

}
</script>

Example 2: How to validate special characters in text box using javascript?

File name : index.php

<input type="text" class="form-control" name="address" id="address" value="<?php echo set_value('address'); ?>" placeholder=" Enter Address"

onkeyup="Validate_specialChar(this.id)" />
<span id="error" style="color: Red; display: none">* Special Characters not allowed.</span>
<span class="required" id="address_errr" aria-required="true"></span>
<span class="error"><?php echo form_error('address'); ?></span>



<textarea class="form-control requireds" name="subject" id="subject" placeholder=" Enter Subject" rows="3" cols="50" onkeyup="ValidatespecialChar(this.id)" maxlength="500" ><?php echo set_value('subject'); ?></textarea>

<span class="error"><?php echo form_error('subject'); ?></span>
<span class="required" aria-required="true" style="color:green;float: right;">Maximum 500 characters are allowed</span></br>
<span class="required" id="subject_errr" aria-required="true"



<script>


function Validate_specialChar(id){
var yourInput = $('#'+id).val();
re = /[`"~!@#$%^&*()?|+\<>\{\}\[\]\\\/]/gi;
var isSplChar = re.test(yourInput);
if(isSplChar){
var no_spl_char = yourInput.replace(/[`"~!@#$%^&*()?|+\<>\{\}\[\]\\\/]/gi, '');
$('#'+id).val(no_spl_char);
$('#'+id+'_errr').text('Special Character not allowed');
}
}






function ValidatespecialChar(id){
var maxLength = 500;
var yourInput = $('#'+id).val();

re = /[`~!@#$%^&*()?|+\<>\{\}\[\]\\\/]/gi;
var isSplChar = re.test(yourInput);

if(isSplChar){
var no_spl_char = yourInput.replace(/[`~!@#$%^&*()?|+\<>\{\}\[\]\\\/]/gi, '');
$('#'+id).val(no_spl_char);
$('#'+id+'_errr').text('Special Character not allowed');
}

var textlen = maxLength - $('#'+id).val().length;
$('#'+id+'_errr').text(textlen+' Character remaining');

}
</script>

<script type="text/javascript">
function ValidatespecialChar(id){
var yourInput = $('#'+id).val();
re = /[`~!@#$%^&*()?|+\<>\{\}\[\]\\\/]/gi;
var isSplChar = re.test(yourInput);
if(isSplChar){

var no_spl_char = yourInput.replace(/[`~!@#$%^&*()|+\<>\{\}\[\]\\\/]/gi, '');
$('#'+id).val(no_spl_char);
}
}



</script>

Special characters validation, Allow alphanumeric , dash, underscore and space on keypress.

File name : index.php

<input onkeypress="return Validate(event);" type="text" /><br/>
<span style="color:red;" id="lblErrorMsg"></span>
<script>
function Validate(e) {
var keyCode = e.keyCode || e.which;
var errorMsg = document.getElementById("lblErrorMsg");
errorMsg.innerHTML = "";

//Regex to allow only Alphabets Numbers Dash Underscore and Space
var pattern = /^[a-z\d\-_\s]+$/i;

//Validating the textBox value against our regex pattern.
var isValid = pattern.test(String.fromCharCode(keyCode));
if (!isValid) {
errorMsg.innerHTML = "Invalid Attempt, only alphanumeric, dash , underscore and space are allowed.";
}

return isValid;
}

</script>





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here