PDA

View Full Version : JS Validation



wkt
November 11th, 2009, 09:00 AM
Hi there

I'm trying to validation on my form which only has 1 element in it. Simple enough but it took me hours, probably because I'm in a highly pissed state.



<html>
<head>
<script type="text/javascript">
function validate_form() {
//with (thisform) {
var id = document.sid.search_id;
if (isInteger(id.value)) {
return true;
} else {
alert("This is not a valid ID!");
return false;
}
//}
}
</script>
</head>
<body>
<form name="sid" onsubmit="return validate_form()">
<input type="text" name="search_id"/>
<input type="submit" value="submit"/>
</form>
</body>
</html>
It doesn't work. When I press submit, it passes the code as if it isn't there. No matter what I put inside the text field, valid or not valid, it doesn't work.

I only have JS experience of less than 24 hours. I'm just trying to get something up and running on my website desperately. Sorry if my question sounds stupid. :eyeup:

Anyway, thanks in advance.

simplistik
November 11th, 2009, 09:41 AM
you need to have an isInteger function

wkt
November 11th, 2009, 10:36 AM
Oh? isInteger itself is not a function? Eeeeel... Thanks. I was right. Stupid question...

//edit: but how can I check whether the value in the text field is an integer then?

actionAction
November 11th, 2009, 11:21 AM
function isInteger (s)
{
var i;

if (isEmpty(s))
if (isInteger.arguments.length == 1) return 0;
else return (isInteger.arguments[1] == true);

for (i = 0; i < s.length; i++)
{
var c = s.charAt(i);

if (!isDigit(c)) return false;
}

return true;
}

prasanthmj
November 11th, 2009, 11:47 AM
Perhaps, this JavaScript form validation script (http://www.javascript-coder.com/html-form/javascript-form-validation.phtml) will save some time.