Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

325
Views
Restrict Sweet Alert input characters Length in Angular

Requirement : The user should not able to type more than 20 characters within the input box.

I have implemented Sweet alert for displaying messages in my Angular project.

Once we click on a button a sweet alert with input type password (popup) will be displayed

I need to restrict the password input to max length of 20

this should happen when the user types not after typing more than 20 characters and then restricting it.

Swal.fire({
    title: "Delete My Account",
    text: "Please enter your password to delete your account",
    input: 'password',
    allowOutsideClick: true,   
    showCloseButton: true,   
    inputAutoTrim:true,
    inputValidator: (value) => {
      if ((value).length > 20) {
        return 'You cannot enter more than 20 characters';
                }          
      else if (!value) {
        return 'Please enter the password';
      }
    }
  }).then((result) => {
      if (result.value) {
          // console.log("Result: " + result.value);
          let password:any = result.value; 
          console.log(" (password).length : " + (password).length );
          if ((password).length > 20) {
            Swal.fire(  'Reached Max',  'You cannot enter more than 20 characters',  'error')
            return false;
        }

          this.userPassword = result.value;
          this.deleteMyAccount();            
      }
      else if (result.isConfirmed) 
      {
        Swal.fire(  'Password required',  'please enter the password',  'error')
      }
  }); 

The above code allows to add N characters to the input and then throwing an error message.

TIA

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You should use the maxlength="20" and onKeyPress="if(this.value.length==20) return false;" in the input tag.

input type="text" class="inputcustom" placeholder="Enter the bank account number" formControlName="accountNumber" maxlength="18" onKeyPress="if(this.value.length==18) return false;">

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!