Presentation is loading. Please wait.

Presentation is loading. Please wait.

JavaScript II. JavaScript Changing Time in the status bar function showTime() { var now = new Date(); var hours = now.getHours(); var minutes = now.getMinutes();

Similar presentations


Presentation on theme: "JavaScript II. JavaScript Changing Time in the status bar function showTime() { var now = new Date(); var hours = now.getHours(); var minutes = now.getMinutes();"— Presentation transcript:

1 JavaScript II

2 JavaScript Changing Time in the status bar function showTime() { var now = new Date(); var hours = now.getHours(); var minutes = now.getMinutes(); var seconds = now.getSeconds(); var timeStr = "" + ((hours > 12) ? hours - 12 : hours); timeStr += ((minutes < 10) ? ":0" : ":") + minutes; timeStr += ((seconds < 10) ? ":0" : ":") + seconds; timeStr += (hours >= 12) ? " P.M." : " A.M."; status = timeStr; // time is displayed in the Status Line setTimeout("showTime()", 1000); }

3 JavaScript Enable & disable select option function disable() { document.getElementById("mySelect").disabled=true;} function enable() { document.getElementById("mySelect").disabled=false;} Apple Banana Orange

4 JavaScript print Output all options function getOptions(){ var x=document.getElementById("mySelect"); var txt="All options: "; var i; for (i=0;i<x.length;i++) { txt=txt + "\n" + x.options[i].text; } alert(txt); } Apple Orange Banana

5 JavaScript print index of selected option function getIndex(){ var x=document.getElementById("mySelect"); alert(x.selectedIndex); } Select your favorite fruit: Apple Orange Banana

6 JavaScript Set text of selected option function changeText() { var x=document.getElementById("mySelect"); x.options[x.selectedIndex].text="Melon"; } Select your favorite fruit: Apple Orange Banana

7 JavaScript Remove selected option function removeOption() { var x=document.getElementById("mySelect"); x.remove(x.selectedIndex); } Apple Banana Orange

8 JavaScript Javascript function to check for all letters in a field function allLetter(inputtxt) { var letters = /^[A-Za-z]+$/; if(inputtxt.value.match(alphaExp)) { return true; } else { alert("message"); return false; } To get a string contains only letters (both uppercase or lowercase) we use a regular expression (/^[A-Za-z]+$/) which allows only letters. Next the match() method of string object is used to match the said regular expression against the input value. Here is the complete web document.

9 JavaScript II Checkbox checked function check(f) { if(f.elements[0].checked&&f.elements[1].checked) { var no=prompt("no of children? "); if(no==2)alert("U r eligible for the loan Rs 1000000"); else alert("U r eligible for loan Rs 50000"); } else alert("not eligible"); } Marital Status Children

10 JavaScript II Login form function validate(form){ var userName = form.Username.value; var password = form.Password.value; if (userName.length === 0) { alert("You must enter a username."); return false; } if (password.length === 0) { alert("You must enter a password."); return false; } Login Form Username: Password:

11 JavaScript II validates that the entry is formatted as an e-mail address function check(elem) { var str = elem.email.value; var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/; if (!str.match(re)) { alert("Verify the e-mail address format."); return false; } else { return true; } } E-mail Address:

12 JavaScript II Check fname is empty function validateForm(f) { var x=f.fname.value; if (x==null || x=="") { alert("First name must be filled out"); return false; } First name:

13 JavaScript II String reversal using charat and length property var my_str="nattop" var i=my_str.length; i=i-1; for (var x = i; x >=0; x--) { document.write(my_str.charAt(x)); }

14 JavaScript II

15 Hide & Show function show1() { document.getElementById('s').style.display='block'; } function show2() { document.getElementById('s').style.display='none'; } Show ME

16 JavaScript II function check() { if(document.getElementById('uname').value==""||document.getElementById('upass').value=="") { if(document.getElementById('uname').value=="") { document.getElementById('suname').style.display='block'; } else { document.getElementById('suname').style.display='none'; } if(document.getElementById('upass').value=="") { document.getElementById('supass').style.display='block'; } else { document.getElementById('supass').style.display='none'; } else { document.f.submit(); } Name Enter Name Passsword Enter Password


Download ppt "JavaScript II. JavaScript Changing Time in the status bar function showTime() { var now = new Date(); var hours = now.getHours(); var minutes = now.getMinutes();"

Similar presentations


Ads by Google