Jump to content

emptyvalidation


yandoo

Recommended Posts

Hi there,

 

i was hoping for a little help please ... I  trying to create some java script validation on a text field so that it cannot be left empty...

 

Heres what i got:

<Script>
function emptyvalidation(entered, alertbox)
{
// Emptyfield Validation by Henrik Petersen / NetKontoret
// Explained at www.echoecho.com/jsforms.htm
// Please do not remove this line and the two lines above.
with (entered)
{
if (value==null || value=="")
{if (alertbox!="") {alert(alertbox);} return false;}
else {return true;}
}
} 
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="tester2222" > 
  <label>
  <input type="text" name="entered" onChange="emptyvalidation(this)">
  </label>
  <label>
  <input type="submit" name="Submit" value="Submit" />
  </label>
</form>

 

Trouble is it doesnt work at all??? I'm using this script direct from tutorial and cant get it working....What am i missing??

 

Thanks

Link to comment
Share on other sites

It's much better (in my opinion) to validate on submit anyways...try this out:

 

<html>
<head>
<script type="text/javascript">
function validate(form){
  if(!form.entered.value.length){
    alert('Please enter a value');
    form.entered.focus();
    return false;
  }
  return true;
} 
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="tester2222" onsubmit="return validate(this);"> 
  <label>
  <input type="text" name="entered" />
  </label>
  <label>
  <input type="submit" name="Submit" value="Submit" />
  </label>
</form>
</body>
</html>

Link to comment
Share on other sites

Hi,

 

Yes i did originally try to validate by submit...I found the validation to work in that the message window appears says Required field...but after closing the little message window the record is updated anyway and the user is directed to another page...??????

 

I would actually prefer to do it by submit like you suggested but it just didnt stop the record being updated and page redirecting...

 

Is that dooable??

 

Thanks :)

 

Link to comment
Share on other sites

The above shouldn't redirect. The key is to have the function return false, and also have a 'return' in front of the function call in the onsubmit. Try the code I posted, it should alert the message, move the cursor to the missing element, and NOT submit

Link to comment
Share on other sites

yes you can...you can test something like form.fieldName.value.length == 3

 

well, my guess is you have an error in your javascript. if the javascript errors out and doesn't return that false, the form will still send. can you post the code you are actually using instead of the sample script?

Link to comment
Share on other sites

<script type="text/javascript">
function validate(form){
  if(!form.entered.value.length){
    alert('Please enter a value');
    form.entered.focus();
    return false;
  }
  return true;
} 
</script>
</head>

<body>
<form id="form1" name="form1" method="post" action="tester2222.php" onsubmit="return validate(this);">
  <label>
  <input type="text" name="entered" />
  
  </label>
  <label>
  <input type="submit" name="Submit" value="Submit" />
  </label>
  
</form>
</body>
</html>

 

That works.....as soon as i add an Update query then it redirects and updates record anyway!

 

if  add the validation to a text field in an existing form i have which updates records... then the vallidation is called, granted but it updates anyway???? All i'm using is an update query ($editformaction).....

 

Please help :)

 

 

 

 

 

 

 

 

 
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
  $updateSQL = sprintf("UPDATE animal SET BreedID=%s, Age=%s, Sex=%s, Image=%s, Neut=%s, Flea=%s, Worm=%s, Treatment=%s, VID=%s  WHERE AnimalID=%s",
                       GetSQLValueString($_POST['select'], "text"),
                       GetSQLValueString($_POST['age'], "int"),
                       GetSQLValueString($_POST['sex'], "text"),
             
                       GetSQLValueString($_POST['image'], "text"),
				   GetSQLValueString (isset($_POST['neut']) ? "true" : "", "defined","1","0"),
				   GetSQLValueString(isset($_POST['flea']) ? "true" : "", "defined","1","0"),
				   GetSQLValueString(isset($_POST['worm']) ? "true" : "", "defined","1","0"),
					GetSQLValueString(isset($_POST['treatment']) ? "true" : "", "defined","1","0"),						GetSQLValueString($_POST['vet'], "text"),
                       GetSQLValueString($_POST['AnimalID'], "int"));

  mysql_select_db($database_woodside, $woodside);
  $Result1 = mysql_query($updateSQL, $woodside) or die(mysql_error());
//Below change to add_animal2
  $updateGoTo = "view_animal_details.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
//$updateGoTo .= '&' . $_POST['AnimalID'];
//}else{
    $updateGoTo .= 'recordID=' . $_POST['AnimalID'];  
//above is new REMOVE
  }
  header(sprintf("Location: %s", $updateGoTo));
}

 

Link to comment
Share on other sites

Hi there,

 

Yes it doenst really make sense, but i'm telling the truth i promise :) I tested it out using a form like above and when i clicked submit (with empty field), the JS message popped up...when i clicked the close window it UPDATED or INSERTED the record anyway and then redirected to page.... (as commanded on either insert r update)

 

I thought that it was wierd behavour....what the hell can it be??? :(

Link to comment
Share on other sites

It's because you are using a form "like above". I am 99% sure you problem is that there is an error in your JS.

 

Do this...

1) Add the following to the end of your validate() function right before the "return true":

alert('Form Complete');

2) Add the following code somewhere on the page inside the BODY (replace form1 with the value of your form's ID attribute)

<input type="button" value="Form Test" onclick="validate(document.getElementById('form1'));" />

3) Click the button. This will test the JavaScript without the form actually submitting. Look for any JavaScript errors. I am willing to bet money there is a JavaScript error.

4) Once you get rid of all your JavaScript errors, remove steps 1 & 2 and you're good to go.

 

If that STILL doesn't work, you will have to post the ACTUAL code you are using to get any help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.