Thứ Sáu, 7 tháng 6, 2019

Giới hạn từ trong input

**** JS:

<!DOCTYPE html>
<html>
<head>
<title>Giới hạn kí tự nhập trong Textarea bằng Javascript</title>
<link href="demo.css" rel="stylesheet" type="text/css"/>
<link rel="icon" href="http://www.thuthuatweb.net/wp-content/themes/HostingSite/favicon.ico" type="image/x-ico"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<script type="text/javascript">
/*=============  This is a count of total number to be displayed ==================*/
var setTotalNumberOfWordCounter = "100";
/*=============  This is a count of total number to be displayed ==================*/

/*This function first get the value of textarea. And read length of that textarea
charactor. then compare the value of set value.*/

function displayWordCounter(){
var getTextValue = document.frm.dd_fc_comment.value;  // Get input textarea value
var getTextLength = getTextValue.length;   // Get length of input textarea value
if(getTextLength > setTotalNumberOfWordCounter){     //compare this length with total count
        getTextValue = getTextValue.substring(0,setTotalNumberOfWordCounter);
        document.frm.dd_fc_comment.value =getTextValue;
        return false;
}
document.frm.totalWordLimit.value = (setTotalNumberOfWordCounter-getTextLength);

}

</script>

</head>
<body>

<form name="frm" method="POST">
<textarea name="dd_fc_comment" class="main_txt_area" onkeyup="return displayWordCounter();"></textarea><br>
<div class="total_count">total remaining Charatctor: <input class="show_count" name="totalWordLimit" size="4" readonly="" value="100" type="text"></div>
</form>

<div class="footer-bar">
    <span class="article-wrapper">
        <span class="article-label">Xem Bài Viết: </span>
        <span class="article-link"><a href="http://www.thuthuatweb.net/javacript/gioi-han-ki-tu-nhap-trong-textarea-bang-javascript.html">Giới hạn kí tự nhập trong Textarea bằng Javascript</a></span>
    </span>
    
</div>
</body>
</html>

## Nhập chữ
$("input[name=txt_phone]").keypress(function(e){
//if the letter is not digit then display error and don't type anything
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
//display error message
$("#errmsg").html("Number").show().fadeOut("slow");
return false;
}

})

Không có nhận xét nào:

Đăng nhận xét