GithubHelp home page GithubHelp logo

lab-08's Introduction

lab-08

function myKeyPress(e){

/* TODO: retrieve the value from the text input */

var mytextbox = "my textbox element";

// TODO: set the value of the textbox with the formatted value

var keyPressed;

if(window.event) { // IE

keyPressed = e.keyCode;

} else if(e.which){ // Netscape/Firefox/Opera

keyPressed = e.which;

}

var x = String.fromCharCode(keyPressed);

var y = formatPhoneNumber("7189515000");

console.log("Key Pressed = " + x);

console.log(" Formatted = " + y);

// TODO: Add a condition to ignore entries beyond 10 digits

}

function formatPhoneNumber(value){

/* TODO: Use replace function to ignore extra - character */

if(value.length > 3 && value.length <= 6)

value = value.slice(0,3) + "-" + value.slice(3);

else if(value.length > 6)

value = value.slice(0,3) + "-" + value.slice(3,6) + "-" + value.slice(6);

return value;

}

<title>Phone Number Masking</title>
<input type="text" id="phone" onkeypress="myKeyPress(this);"/>
<script> function myKeyPress(e){ /* TODO: retrieve the value from the text input */ var mytextbox = document.getElementById('phone').value // TODO: set the value of the textbox with the formatted value var keyPressed; if (window.event) { // IE keyPressed = e.keyCode; } else if (e.which) { // Netscape/Firefox/Opera keyPressed = e.which; } var x = String.fromCharCode(keyPressed); var y = formatPhoneNumber(mytextbox); console.log("Key Pressed = " + x); console.log(" Formatted = " + y); // TODO: Add a condition to ignore entries beyond 10 digits if (y.length>11){ document.getElementById('phone').value = y.slice(0, 12)//only first 12 including hypens event.preventDefault(); //not accepting any more characters }else{ document.getElementById('phone').value = y } } function formatPhoneNumber(value) { /* TODO: Use replace function to ignore extra - character */ //splitting the values after 3 and 7 characters if(value.length == 3){ value = value.slice(0, 3) + "-"; }else if (value.length > 4 && value.length < 7){ value = value.slice(0, 3) + "-" + value.slice(4); }else if (value.length == 7){ value = value.slice(0, 3) + "-" + value.slice(4, 7) + "-"; }else if (value.length > 7){ value = value.slice(0, 3) + "-" + value.slice(4, 7) + "-" + value.slice(8); } return value; } </script>

lab-08's People

Contributors

mercurydon007 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.