Número de teléfono Validación de Javascript
Frecuentes
Visto 1,215 equipos
-2
Hello I am having trouble with my javascript validation. I am trying to validate a Phone number which I want it to display only numbers and single spaces.
Mi código Javascript:
<script type="text/javascript">
function validateForm()
{
var x=document.forms["checkout_details"]["phone"].value;
if (!isNaN(phone).value)//if the entered phone number is not a number
{
alert("Please enter a valid phone number");
ret = false;//return false, form is not submitted
}
}
</script>
HTML/PHP Code:
echo '<form name="checkout_details" action="confirm.php" onsubmit="return validateForm()" method="post">';
echo '<font color="red">*</font> <b>Phone Number:</b> <input type="text" name="phone" id="phone"><br /><br />';
echo '<input type="submit" value="Purchase">';
</form>
Anyone help me out tell me what i'm doing wrong. Thanks.
3 Respuestas
0
Esto es incorrecto:
if (!isNaN(phone).value)//if the entered phone number is not a number
That actually evaluates to if it IS a number (double negative makes a positive). Also, ret = false;
debiera ser return false;
To remove spaces from the input before checking if it is valid, see esta respuesta.
contestado el 23 de mayo de 17 a las 11:05
0
Prueba esta
function validateForm()
{
var x=document.forms["checkout_details"]["phone"].value;
if (!isNaN(x))
{
alert("Please enter a valid phone number");
return false;
}
else
return true;
}
contestado el 28 de mayo de 14 a las 14:05
0
Su función necesita volver true
or false
. La false
return will prevent your form from posting.
Así que cambia esto:
ret = false;//return false, form is not submitted
para:
return false;//return false, form is not submitted
Additionally, you cannot rely on isNaN to validate this field. In addition to not taking into account any phone numbers with spaces or other characters, it will not work correctly en algunos casos, and in this case you are negating its return value (!isNaN(x)
), which means it won't work at all unless you fix that. You are better off using a regular expression to validate this field.
contestado el 28 de mayo de 14 a las 15:05
Additionally, I wouldn't validate phone numbers by checking if the entered value is not a number. This will invalidate any phone number entered with spaces, (
, )
, +
, etc - El Gavilán
Hello i tried this but it still doesn't work. Any suggestions? - user3674827
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas javascript html or haz tu propia pregunta.
I want to purchase something, but none of my phone numbers can be written using only numbers and spaces. Well, I think I'll go purchase the product somewhere else. - Arseni Mourzenko
your function doesn't return anything - Alexandru Chichinete
What am i missing? How do i fix it? - user3674827
First, you need to decide on what you believe are acceptable characters. Numbers only as in 1234567890? Are spaces allowed? Hyphens allowed? Then, you can use javascript regex to check for the proper format - The One and Only ChemistryBlob
@MainMa What format is your number in? NANP only requires numbers (doesn't even need spaces). - Alex W