Character limit validation in javascript

How to validate character limit in javascript?

Demo Example

File name : index.php

<html>

<head>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js">
</script>

</head>
<body>

<div class="col-md-8">
<textarea class="form-control requireds" id="query_remark" name="query_remark" rows="3" cols="50" onkeyup="ValidatespecialChar(this.id)" maxlength="1500"></textarea><br/>
<span class="required" aria-required="true" style="color:green;">Maximum 1500 characters are allowed</span></br>
<span class="required" id="query_remark_errr" aria-required="true"></span></br>
</div>


<script>

function ValidatespecialChar(id){

var maxLength = 1500;
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);
}

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

}
</script>

</body>


</html>





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here