UNPKG

programacion-web

Version:

validacion-formularios

20 lines (17 loc) 588 B
function convertir() { const celsiusInput = document.getElementById('celsiusInput'); const resultado = document.getElementById('resultado'); const alerta = document.getElementById('alerta'); const valor = celsiusInput.value.trim(); const regex = /^-?\d+(\.\d+)?$/; if (!regex.test(valor)) { alerta.classList.remove('d-none'); resultado.value = ""; return; } else { alerta.classList.add('d-none'); } const celsius = parseFloat(valor); const fahrenheit = (celsius * 9/5) + 32; resultado.value = fahrenheit.toFixed(1) + " °F"; }