-1

Alright, so I feel my issue is that of a really simple one. basically, I'v created a website, and am trying to use javascript to validate input, all of which initially was one. When input fields were left blank, an error would prompt and so forth. I haven't changed a single thing, and now no form of validation takes place, and it simply ignores my javascript. Any ideas?

Here's an example of the Javascript;

function validate(el){
var alphabets="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
var temp;

if (el.Full_Name.value == "") {
     alert("Cannot leave area field blank!");
     return false;
 }
}

for (var i=0;i<el.Full_Name.value.length;i++){
temp=el.Full_Name.value.substring(i,i+1)
if (alphabets.indexOf(temp)==-1){
alert("Sorry, your name contains a " + temp +", which is not allowed.")
return false
   }
  }

Here's the HTML:

<form name="testform" onSubmit="return validate(testform)">
<table width="650" border="0">

<tr>
 <td width="79" valign="top">
  <label for="Full_Name">Full Name<span class="required_star"> * </span></label>
 </td>
 <td width="289" valign="top">
  <input size="15" type="text" name="Full_Name" id="Full_Name" maxlength="50" value="" />
 </td>
</tr>
<tr>
 <td colspan="4" style="text-align:center">
  <input type="submit" value="Proceed"/>
 </td>
</tr></table>
</form>
5
  • Any errors in the Javascript console? Commented Sep 14, 2015 at 0:41
  • Is your input actually in a form. Commented Sep 14, 2015 at 0:42
  • please post the HTML so we can have a better view. Commented Sep 14, 2015 at 0:43
  • I don't seem to be getting any errors in the console. It's just really frustrating me that it worked, and now all of a sudden, unless i accidentally changed something without realising Commented Sep 14, 2015 at 0:53
  • document.form.Full_Name.value should be document.forms.testform.Full_Name.value. document.forms is a collection, you can access the members by name (if they have one) or index (the form you seek seems to be the first, so document.forms[0]). Commented Sep 14, 2015 at 1:03

2 Answers 2

2

You can speed up/simplify things, a bit:

<form name="testform" onSubmit="return validate(this)">

Notice use of 'this' - it actually refers to form element itself...

JS:

 function validate(el) {
if (el.Full_Name.value == "") {
     alert("Cannot leave area field blank!");
     return false;
 }
}

Demo: http://jsfiddle.net/tv9hntkL/

P.S. Correct selector in your case, would be: (document.testform.Full_Name.value == "")

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the response! I just tried to implement this as you have done, and still no luck. On selection of the submit button it just proceeds as if theres no javascript whatsoever
@Philip Newton, One syntax error (at least), check it now: jsfiddle.net/tv9hntkL/1 Redundant closing bracket '}' was problem....
Worked absolutely incredibly. Just one more follow question if it's alright? If i wanted to validate that the user's input was lets say, more than 5 characters long, how would i go about doing that?
Np, glad it worked, finally. To limit length, you can do something like this: jsfiddle.net/tv9hntkL/2
You've been exceptionally helpful! I don't want to bother you with another question, but i feel it is necessary hahah! If i later wanted to input radio buttons, or the likes of a drop-down menu, is validation possible for this?
|
1

You are accessing the form input wrong.

var myForm =  document.forms.<form-name>;
myForm.<input-name>.value; // holds the value of a certain input

will give you access to the value correctly.

2 Comments

Thanks for the quick response. I implemented alike the way you suggested, and still nothing: document.forms.testform.Full_Name.value=="".
please provide your HTML in your original post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.