Stinger100
03-02-2006, 08:26 PM
hi,
I have just started learning Javascript. I was just wondaring if anyone can spot what is wrong with the below code i have done as it doesnt seem to work.
<html>
<head>
<script type="text/javascript">
var valid = 'abcdefghijklmnopqrstuvwxyz';
function CheckFieldA(fieldA){
if(!isValid(fieldA.value,valid)){
alert("invalid format for field a")
}
if(fieldA.value.length>50){
alert("a field should not be greater than 50 characters")
}
}
function checkFieldB(fieldB){
if(fieldB.substring(0,6)!="http://" || fieldB.substring ((fieldB.value.length-4),fieldB.value.length)){
alert("Filed b should be in http://mywebsite.com")
}
} function isValid(string,allowed) {
for (var i=0; i< string.length; i++) {
if (allowed.indexOf(string.charAt(i)) == -1)
return false;
}
return true;
}
</script>
</head>
<body>
<form name="myform" method="post"
onSubmit="return CheckFieldA(document.myform)">
<pre>
Name of Hyperlink:<input type="text" name="fieldA")>
Category of Hyperlink: <input type="text" name="fieldB">
Hyperlink: <input type="text" name="Hyperlink">
Keyword <input type="text" name="Keyword">
Date <input type="text" name="Date">
</pre>
<input type="submit" name="submit" value="Submit">
</form>
thanks in advance.
Stinger
I have just started learning Javascript. I was just wondaring if anyone can spot what is wrong with the below code i have done as it doesnt seem to work.
<html>
<head>
<script type="text/javascript">
var valid = 'abcdefghijklmnopqrstuvwxyz';
function CheckFieldA(fieldA){
if(!isValid(fieldA.value,valid)){
alert("invalid format for field a")
}
if(fieldA.value.length>50){
alert("a field should not be greater than 50 characters")
}
}
function checkFieldB(fieldB){
if(fieldB.substring(0,6)!="http://" || fieldB.substring ((fieldB.value.length-4),fieldB.value.length)){
alert("Filed b should be in http://mywebsite.com")
}
} function isValid(string,allowed) {
for (var i=0; i< string.length; i++) {
if (allowed.indexOf(string.charAt(i)) == -1)
return false;
}
return true;
}
</script>
</head>
<body>
<form name="myform" method="post"
onSubmit="return CheckFieldA(document.myform)">
<pre>
Name of Hyperlink:<input type="text" name="fieldA")>
Category of Hyperlink: <input type="text" name="fieldB">
Hyperlink: <input type="text" name="Hyperlink">
Keyword <input type="text" name="Keyword">
Date <input type="text" name="Date">
</pre>
<input type="submit" name="submit" value="Submit">
</form>
thanks in advance.
Stinger