What's new

Closed Javascript code textbox validation

Status
Not open for further replies.

Generation Miracles

Forum Guru
Joined
Jan 21, 2018
Posts
4,737
Solutions
5
Reaction
1,076
Points
1,214
Age
27
hello mga ka phc pa help nito sa code ko.ang problema kasi nito pag space input ko proceed to need step siya ang labas dapat sana value is empty.ano mali dito sa code mga ka phc?ito po code ko .


<!DOCTYPE html>
<html>

<body>

<h1>Textbox Validation</h1>


<input type="text" id="txt1">
<button onclick="val_txt();">Validate Text box</button>

<script>
function val_txt()
{
var txtval = document.getElementById('txt1').value;
if (txtval == "")
{
alert("Value is empty");
return false;
}
else
{
alert("Proceed to next step");
}
}
</script>

</body>

</html>
 
if (txtval == false)

itry nyo po ito sa if condition nyo
 
Last edited:
pwde mo po itry ung trim function ng javascript :)
Code:
var txtval = document.getElementById('txt1').value.trim();
 
Try mo nga ito paps. Hindi ako sure kung tama...

Code:
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Validate User's Input</title>
  <style>
    .container {
      width: 80%;
      margin: 0 auto;
      border: 1px solid #000000;
    }
    h1, #result {
      font-family: Arial, Helvetica, sans-serif;
      text-align: center;
    }
    #result {
      color: #ff0000;
      font-size: 1.1em;
      font-family: Arial, Helvetica, sans-serif;
    }
    .form {
      display: flex;
      align-items: center;
      justify-content: center;
      padding: 15px;
    }
  </style>
</head>
<body>

  <div class="container">
      <h1>Textbox Validation</h1>
  <div class="form">
    <input type="text" id="userInput" placeholder="Enter a value" required />
    <button id="validate">Validate Text box</button>
  </div>
  <p id="result"></p>
  </div>

  <script>
       
      validate.addEventListener('click', (e) => {
       
        'use strict'
       
        e.preventDefault()
       
        const spaces = /^\s*$/;
        const userInput = document.getElementById("userInput").value;
        const result = document.getElementById("result");

        if (userInput == "" || spaces.test(userInput)) {
          result.innerHTML = "Field must not be empty and space is not allowed!"
        } else {
          result.innerHTML = "Proceed to next step";
        }

      })


  </script>

</body>
</html>
 

Attachments

Try mo nga ito paps. Hindi ako sure kung tama...

Code:
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Validate User's Input</title>
  <style>
    .container {
      width: 80%;
      margin: 0 auto;
      border: 1px solid #000000;
    }
    h1, #result {
      font-family: Arial, Helvetica, sans-serif;
      text-align: center;
    }
    #result {
      color: #ff0000;
      font-size: 1.1em;
      font-family: Arial, Helvetica, sans-serif;
    }
    .form {
      display: flex;
      align-items: center;
      justify-content: center;
      padding: 15px;
    }
  </style>
</head>
<body>

  <div class="container">
      <h1>Textbox Validation</h1>
  <div class="form">
    <input type="text" id="userInput" placeholder="Enter a value" required />
    <button id="validate">Validate Text box</button>
  </div>
  <p id="result"></p>
  </div>

  <script>
      
      validate.addEventListener('click', (e) => {
      
        'use strict'
      
        e.preventDefault()
      
        const spaces = /^\s*$/;
        const userInput = document.getElementById("userInput").value;
        const result = document.getElementById("result");

        if (userInput == "" || spaces.test(userInput)) {
          result.innerHTML = "Field must not be empty and space is not allowed!"
        } else {
          result.innerHTML = "Proceed to next step";
        }

      })


  </script>

</body>
</html>
thanks ts
 
Status
Not open for further replies.

Similar threads

Back
Top