Tillåt bara nummer i fält

JavaScript

5 svar · 348 visningar · startad av Wiklund!!

Medlem sedan juli 2002549 inlägg
Trådstart#1

Har denna kod:

<script language="javaScript">
function personal()
{
if (document.post1.fname.value=="")
{
alert("You have to enter your first name!");
return false;
} else if (document.post1.lname.value=="")
{
alert("You have to enter your last name!");
return false;
} else if (document.post1.city.value.length<3)
{
alert("The name of the city where you live cannot be less than 3 characters!");
document.post1.city.focus();
return false;
}
}
</script>

Vill lägga till så att den kollar ett fält (mobile) så att det bara innehåller nummer. Hur gör jag det?

Medlem sedan dec. 20025 483 inlägg
#2
<script language="javaScript">
function personal()
{
if (document.post1.fname.value=="")
{
alert("You have to enter your first name!");
return false;
} else if (document.post1.lname.value=="")
{
alert("You have to enter your last name!");
return false;
} else if (document.post1.city.value.length<3)
{
alert("The name of the city where you live cannot be less than 3 characters!");
document.post1.city.focus();
return false;
} else if (!/^\d{10}$/.test(document.post1.mobile.value)){
    alert("...");
    document.post1.mobile.focus();
    return false;
}
return true;
}
</script>
Medlem sedan juli 2002549 inlägg
#3

hmm fungerade inte.. får inga scriptfel men den säger inte ifrån när jag skriver text i fältet.

Medlem sedan dec. 20025 483 inlägg
#4

Hoppsan! Jag glömde ett tecken. Testa nu.

Medlem sedan juli 2002549 inlägg
#5

Fungerar nu tack.
Sorry om jag är jobbig men går det att göra så att om fältet är tomt så körs inte kontrollen. Vore guld värt!

Medlem sedan dec. 20025 483 inlägg
#6
<script language="javaScript"> function personal() { 
if (document.post1.fname.value=="") { 
alert("You have to enter your first name!"); 
return false; 
} else if (document.post1.lname.value=="") { 
alert("You have to enter your last name!"); 
return false; 
} else if (document.post1.city.value.length<3) { 
alert("The name of the city where you live cannot be less than 3 characters!"); document.post1.city.focus(); return false; 
} else if (document.post1.mobile.value.length != 0 && !/^\d{10}$/.test(document.post1.mobile.value)){ 
alert("..."); 
document.post1.mobile.focus(); 
return false; } 
return true; } 
</script>
259 ms totalt · 4 externa anrop · v20260731065814-full.d34c6d5a
126 ms — deklarationer (db)
0 ms — hämta statistik (cache)
129 ms — hämta tråd, inlägg och bilagor (db)
125 ms — ändringar (db)