Monday, 7 September 2015

Coding in JS

################Accept Numbers
function digitsOnly(event) {
var keynum;
if (window.event) // IE
{
keynum = event.keyCode;
} else if (event.which) // Netscape/Firefox/Opera
{
keynum = event.which;
}
if (keynum >= 48 && keynum <= 57 || keynum == 9 || keynum == 8
|| keynum == 127 || keynum == '' || keynum == null) {
return true;
}
return false;
}
############allow single value after dot
onkeyup="return allowMaxFractionDigits(this,event,1);"
function allowMaxFractionDigits(object, event, maxFractions) {
key = event.keyCode;

// control keys
if(key <= 27 || (key >=33 && key <=40) || key == 45 || key == 46) {
return true;
}

var value = object.value;
if (value != null) {

var dotPosition = value.indexOf('.');

if (dotPosition != -1
&& ((value.length - 1) - dotPosition > maxFractions)) {
object.value = value.substring(0, (value.length - 1));
return false;
}
}
return true;
}
###############Numbers with single dot
function numbersWithSingleDotonly(actualElement, e)
{
var unicode=e.charCode? e.charCode : e.keyCode;
if (unicode ==  190 && actualElement.value.indexOf('.') < 0)
{
if (actualElement.value == "")
{
actualElement.value = "0";
}
    return true;
}
else
{
if (unicode == 8 || unicode == 9) return true;

    if (unicode >=48 && unicode <= 57)
  {
      return true;
  }
}
     return false;
}
##########Clear text field values
 function clearSuspectedDrugOtherProvider()
 {
  var otherProviderText = document.getElementById('drugReactionDetailsForm:SuspectedDrugsSection:ADRDetailsSuspectedDrugsSectionPrescribedByProviderFilterIdOtherProviderInputID');
if(null!=otherProviderText){
otherProviderText.value="";
}
 }
###########How to find the drop list value in javascript
<!DOCTYPE html>
<html>
<head>
<script>
function copyText()
{
var e = document.getElementById("abc");
var strUser = e.options[e.selectedIndex].text;
alert(strUser);
}
</script>
</head>
<body>

Field1: <select id="abc">
<option value="1">Volvo</option>
<option value="2">Saab</option>
<option value="3">Mercedes</option>
<option value="4">Audi</option>
</select>
<br>
<button onclick="copyText()">Select Text</button>

</body>
</html>

No comments:

Post a Comment