Coder Social home page Coder Social logo

willywes / w2-rut-validator Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 6 KB

Una librería javascript para validar, formatear y generar el dígito verificador

License: MIT License

JavaScript 100.00%
chile chileno formater run rut verificar cedula react rut-validator validation

w2-rut-validator's Introduction

W2 Rut Validator

  • Return true or false for format and unformat RUT
  • Return format and unformat string

To Install

npm i w2-rut-validator

How to use

import { RutValidator } from "w2-rut-validator"

Validate
RutValidator.validate(rut) // true or false
Format
RutValidator.format('111111111') // return 11.111.111-1
UnFormat
RutValidator.unformat('11.111.111-1') // return 111111111
Get Dv
RutValidator.getDv(rut) // return check digit
IsValid

Validate Regex [^0-9Kk] and 999.999.999-9 max length

RutValidator.isValid(rut) // return true or false

Basic Example

Validate
RutValidator.validate('111111111') // return true
RutValidator.validate('11.111.111-1') // return true
RutValidator.validate('111111112') // return false
RutValidator.validate('11.111.111-2') // return false
GetDv
RutValidator.getDv('11111111') // return 1
RutValidator.getDv('11.111.111') // return 1
RutValidator.getDv('22222222') // return 2*
RutValidator.getDv('22.222.222') // return 2
Format and Validate
let rut = '111111111' // or 111111111
RutValidator.validate(RutValidator.format(rut)) // return true

let rut = '111111112' // or 111111112
RutValidator.validate(RutValidator.format(rut)) // return false

let rut = 'ASD-F'
RutValidator.isValid(rut) // return false

React Example

import React, { useState } from "react";
import { RutValidator } from "w2-rut-validator"

const RegisterForm = () => {

  const [rut, setRut] = useState('');

  const RutFormat = e => {
    if(RutValidator.isValid(e.target.value)){
      setRut(RutValidator.format(e.target.value))
    }
  }
   
  const RutValidate = e => {
    if(!RutValidator.validate(e.target.value)){
        alert('El RUT ingresado no es correcto, intentalo denuevo.');
        setRut('')
        e.target.focus()
    }
  }

  return (
    <div>
      <input
        type="text"
        onChange={e => setRut(e.target.value)}
        onKeyDown={RutFormat}
        placeholder="19123456-9"
        onBlur={RutValidate}
        value={rut}
      />
    </div>
  );
};

export default RegisterForm;

w2-rut-validator's People

Contributors

willywes avatar

Stargazers

 avatar

Watchers

 avatar  avatar

w2-rut-validator's Issues

Importación no funciona

Exportas un default asi que se debe requerir como:

import RutValidator from "w2-rut-validator"

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.