Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
81
rated 0 times [  87] [ 6]  / answers: 1 / hits: 21138  / 12 Years ago, tue, july 17, 2012, 12:00:00

I'm been trying to validate my fields by using 'getElementById()' with '.value'. However, it seems like either getElementById.value is not working or some codes has overlap the function.



Updated Javascript function:



function validate() {
var Name = document.getElementById('Name').value;
var Tel = document.getElementById('Tel').value;
var FaxNo = document.getElementById('FaxNo').value;
if (Name != ) //wanted to check for alphabets only.
{
alert(Correct);
return true; //doesnt go to next.php
}
else
{
alert(Don leave blank!)
return false;
}
if (isNaN(Tel)) //check only numbers. Same code for FaxNo.
{
alert(Correct);
return true; //doesnt go to next.php
}
else
{
alert(invalid);
return false
}

return true; //doesn't go to next.php
}


My Form:



<Form action =next.php method=post>
<input name=Name type=text id=Name value=/>
<input name=Tel type=text id=Tel value=/>
<input name=FaxNo type=text id=FaxNo value=/>
<input type=submit name=submit onclick=return validate();/>
</Form>


I have already defined my onclick function to my Javascript and tried to add return false too. But the alert still cant appear. Kindly advise.


More From » html

 Answers
56

Your markup is invalid:



<input name=Name type=text id=Name  value=/>
^-----------should be removed


so correction would be removing all extra characters:



<input name=Name type=text id=Name value=/>
<input name=Name type=text id=Name value=/>
<input name=Tel type=text id=Tel value=/>
<input name=FaxNo type=text id=FaxNo value=/>


For preventing submition,when input is invalid, you can try something like a:



function validate() {
var Name = document.getElementById('Name').value;
var Tel = document.getElementById('Tel').value;
var FaxNo = document.getElementById('FaxNo').value;

if (Name != ) //wanted to check for alphabets only.
alert(Correct)
else {
alert(Don leave blank!)
return false;
}

if (isNumeric(Tel)) //check only numbers. Same code for FaxNo.
alert(Correct)
else {
alert(invalid);
return false;
}
}
//Borrowed from jQuery lib
function isNumeric( obj ){
return !isNaN( parseFloat(obj) ) && isFinite( obj )
}

<input type=submit name=submit onclick=return validate()/>

[#84200] Monday, July 16, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
anitamaeg

Total Points: 466
Total Questions: 106
Total Answers: 106

Location: Suriname
Member since Sun, Jun 13, 2021
3 Years ago
;