
Aquí te pongo algunos ejemplos de uso de expresiones regulares:
Validar un número de teléfono
<!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8"> <title>Ejemplo de RegExp</title> <script type="text/javascript"> const iniciar = () => { document.getElementById("comprobar").onclick = comprobarTelefono; } const comprobarTelefono = () => { let telefono = document.getElementById("telefono").value; let patron = /^\d{9}$/; if (telefono.match(patron)) alert('Teléfono Correcto!!!!'); else alert('Teléfono INCORRECTO!!!!'); } window.onload = iniciar; </script> </head> <body> <form name="formulario"> <label for="telefono">Teleono:</label> <input type="text" name="telefono" id="telefono" /> <input type="button" name="comprobar" id="comprobar" value="Comprobar Formato" /> </form> </body> </html>
Validación de un número de DNI
<!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8"> <title>Ejemplo de RegExp</title> <script type="text/javascript"> const iniciar = () => { document.getElementById("comprobar").onclick = comprobarDni; } const comprobarDni = () => { let dni = document.getElementById("dni").value; let patron = /^\d{8}[A-Z]$/; if (dni.match(patron)) alert('DNI Correcto!!!!'); else alert('DNI INCORRECTO!!!!'); } window.onload = iniciar; </script> </head> <body> <form name="formulario"> <label for="dni">DNI:</label> <input type="text" name="dni" id="dni" /> <input type="button" name="comprobar" id="comprobar" value="Comprobar Formato" /> </form> </body> </html>