Skip to content

Latest commit

 

History

History
225 lines (185 loc) · 4.69 KB

File metadata and controls

225 lines (185 loc) · 4.69 KB

JavaScript Input Handling

1. Input Methods in JavaScript

1.1 Browser-Based Input Methods

  1. Prompt Input
  2. Confirm Input
  3. Alert Interaction
  4. HTML Form Inputs
  5. DOM Element Interaction

2. Prompt Method

Basic Syntax

let userName = prompt("Enter your name:", "John Doe");

Characteristics

  • Simple built-in browser dialog
  • Returns user input as a string
  • Optional default value
  • Returns null if user cancels

Example

// Full prompt example
function greetUser() {
    let name = prompt("What's your name?", "Guest");
    
    if (name === null) {
        alert("User cancelled input");
    } else {
        alert(`Hello, ${name}!`);
    }
}

3. Confirm Method

Basic Syntax

let result = confirm("Are you sure?");

Characteristics

  • Returns boolean value
  • Shows OK/Cancel dialog
  • Used for yes/no decisions

Example

function deleteItem() {
    let confirmation = confirm("Do you want to delete this item?");
    
    if (confirmation) {
        console.log("Item deleted");
    } else {
        console.log("Deletion cancelled");
    }
}

4. HTML Form Inputs

4.1 Text Input

<input type="text" id="username" name="username">
<button onclick="processInput()">Submit</button>

<script>
function processInput() {
    let username = document.getElementById('username').value;
    console.log(username);
}
</script>

4.2 Number Input

<input type="number" id="age" name="age">
<script>
function validateAge() {
    let age = document.getElementById('age').value;
    let numAge = Number(age);
    
    if (numAge >= 18) {
        console.log("You are an adult");
    } else {
        console.log("You are a minor");
    }
}
</script>

5. Input Validation Techniques

5.1 Basic Validation

function validateInput(input) {
    // Check if input is empty
    if (input.trim() === "") {
        alert("Input cannot be empty");
        return false;
    }
    
    // Check input length
    if (input.length < 3) {
        alert("Input too short");
        return false;
    }
    
    return true;
}

5.2 Regular Expression Validation

function validateEmail(email) {
    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    return emailRegex.test(email);
}

function checkEmail() {
    let email = document.getElementById('email').value;
    
    if (validateEmail(email)) {
        console.log("Valid email");
    } else {
        console.log("Invalid email");
    }
}

6. Advanced Input Methods

6.1 Event Listeners

// Real-time input tracking
let inputField = document.getElementById('realTimeInput');

inputField.addEventListener('input', function(event) {
    console.log("Current input:", event.target.value);
});

6.2 Keyboard Events

document.addEventListener('keydown', function(event) {
    console.log("Key pressed:", event.key);
    
    // Custom key handling
    if (event.key === 'Enter') {
        processSubmission();
    }
});

7. Type Conversion Techniques

7.1 Conversion Methods

let stringInput = "42";

// Convert to Number
let numericValue = Number(stringInput);  // 42
let parsedValue = parseInt(stringInput); // 42
let floatValue = parseFloat(stringInput); // 42.0

// Convert to Boolean
let booleanValue = Boolean(stringInput); // true

// Convert to String
let stringValue = String(numericValue);  // "42"

8. Error Handling

function safeInputProcess(input) {
    try {
        // Processing logic
        let result = someComplexCalculation(input);
        return result;
    } catch (error) {
        console.error("Input processing error:", error);
        return null;
    }
}

9. Security Considerations

  1. Always sanitize and validate user inputs
  2. Use trim() to remove unnecessary whitespaces
  3. Implement server-side validation
  4. Escape special characters
  5. Set input length limits

10. Modern Input Techniques

10.1 Fetch API with Form Data

async function submitForm(event) {
    event.preventDefault();
    
    let formData = new FormData(event.target);
    let data = Object.fromEntries(formData);
    
    try {
        let response = await fetch('/submit', {
            method: 'POST',
            body: JSON.stringify(data)
        });
    } catch (error) {
        console.error("Submission failed");
    }
}

Conclusion

Mastering input handling is crucial for creating interactive web applications. Practice, validate, and always prioritize user experience and security.

Pro Tips:

  • Validate all inputs
  • Provide clear feedback
  • Handle edge cases
  • Use appropriate input types

Happy Coding! 🚀👨‍💻