Link to home
Create AccountLog in
Avatar of mindfully
mindfully

asked on

Javascript Help: form validation question?

I had EE assistance on the below javascript.  It works perfectly - but now I need to add some additional form validation in and i wasn't sure how to do that without impacting this.  Is there a way to restrict anything in the text box except for the words "I accept" in the text box? Or only allow these specific words to be admitted?

It only checks if there is text entered so anything can be entered into the text field and then click to proceed. I want to restrict someone from just entering anything they want. I only want to allow the words "I accept" in the text box. Is this doable?

Still new to this so your assistance is greatly appreciated. Thanks.


<html>
<head>

<script>
function validate(theForm) {
  if (theForm.textbox.value=='' || theForm.textbox.value==theForm.textbox.defaultValue) {
    alert("You did not enter 'I Accept' in the textbox. Please enter before proceeding. \n");
    theForm.textbox.focus();
    return false;
  }
  return true
}
</script>
</head>
<body>

<form onSubmit="return validate(this)">
<input name="textbox" type="text"
value="Click here to type"
onfocus="if (this.value==this.defaultValue) this.select()"
onblur="if (this.value=='') this.value=this.defaultValue">
<input type="submit">
</form>

</body>
</html>

Avatar of KNVB HK
KNVB HK
Flag of Hong Kong image

try the following:
<html>
<head>

<script>
function validate(theForm) {
  if (theForm.textbox.value=='' || theForm.textbox.value==theForm.textbox.defaultValue || theForm.textbox.value=="I accpet") 
 {
    alert("You did not enter 'I Accept' in the textbox. Please enter before proceeding. \n");
    theForm.textbox.focus();
    return false;
  }
  return true
}
</script>
</head>
<body>

<form onSubmit="return validate(this)">
<input name="textbox" type="text"
value="Click here to type"
onfocus="if (this.value==this.defaultValue) this.select()"
onblur="if (this.value=='') this.value=this.defaultValue">
<input type="submit">
</form>

</body>
</html>

Open in new window

SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
cstsang you meant

 if (theForm.textbox.value=='' || theForm.textbox.value==theForm.textbox.defaultValue || theForm.textbox.value!="I accept")

but it is not necessary to test the preceding
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of mindfully
mindfully

ASKER

Thanks guys for your help. Worked like a charm.
I split up the points and awarded some points to cstsang for his participation.
mplungjan you rock!

Thanks again.
You are very welcome :)

Not to be picking nits or trying to be superior, but both cstsang's suggestions were incorrect.