Saturday, July 9, 2016

js validation in a form and set focus


<html>
<head>
<script type="text/javascript">

function validate(){
var eduInput = document.getElementsByName('edu[]');
var name4=document.getElementById('name4').value;
for (i=0; i<eduInput.length; i++) {
if (eduInput[i].value == "")
{
alert('Complete all the fields');
document.getElementById(eduInput[i].id).focus();
return false;
}
}
if(name4==''){
alert('Enter name field');
document.getElementById('name4').focus();
return false;
}
}

</script>
</head>
<body>
<form onsubmit="return validate()">
Education:<br>
<input type="text" name="edu[]" id="name1" placeholder="name array fields" /><br>
<input type="text" name="edu[]" id="name2" placeholder="name array fields" /><br>
<input type="text" name="edu[]" id="name3" placeholder="name array fields" /><br>
<input type="text" name="edu" id="name4" placeholder="name" /><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

using page break in print content

to print content with new page or putting a page break in the page we use css styling.
syntax: page-break-after: auto|always|avoid|left|right|initial|inherit;

Property Values

Value Description
auto(Default) Automatic page breaks
always Always insert a page break after the element
avoid Avoid page break after the element (if possible)
left Insert page breaks after the element so that the next page is formatted as a left page
right Insert page breaks after the element so that the next page is formatted as a right page
initial Sets this property to its default value.
inherit Inherits this property from its parent element.

example:
@media print {
    .newPageClass{ page-break-after: always; }
}
and use this class in the div after which you want a page break while printing the page

Monday, July 4, 2016

js to print only a specific div

We always have to print a page. Many time we create a separate page for print but by using this code we can print any div. Just we need to put an id in the div and while calling the print function pass this id and cheers.....................


<div id="printableArea">
      <h1>Print me</h1>
</div>

<input type="button" value="Print" class="submitbutton" onClick="printContent('feePrint');">

<script type="text/javascript">
function printContent(el){
var restorepage = $('body').html();
var printcontent = $('#' + el).clone();
$('body').empty().html(printcontent);
window.print();
$('body').html(restorepage);
}
</script>