Jump to content

require field not working


jeff5656

Recommended Posts

I use formvalidator for required fields in a form.  It is not working; when I submit the form, it allows the form to be processed even if the field is blank.

 

Here's the script in the header:

<script type="text/javascript" src="../consults/formvalidator.js"></script>
<script type="text/javascript">
<!--
function check(formname, submitbutton) {
  var errors = '';
  
  errors += checkText(formname, 'close_reason', 'Specify action done for this notification');
    
  return checkThisForm(formname, submitbutton, errors);
}

//-->
</script>

 

Here is the form:

 

<form name="updaterecord" method="post" action="updaterecord.php"> 
  <input type="hidden" name ="id_incr" value="<?php echo $row['id_incr']; ?>"/>
  <input name="close_reason" type="text" size="20" />
  <input type="submit" value="Close" onClick="return check('updaterecord', this.name)"/>
  </form>

 

When I hit submit, I want it to display the error if close_reason is blank.

I have the formvalidator.js in the correct directory and my other forms work correctly with this js file.  Thank you.

 

Here is formvalidator.js in case you need that:

 

function checkThisForm(formname, submitbutton, errors) {
  if (errors == '') {
    //eval(formname+'.'+submitbutton+'.disabled=true');
    //eval('document.'+formname+'.submit()');
    return true;
  } else {
    alert(errors);
    return false;
  }
}

function checkText(formname, textboxname, displaytext) {
  var localerror = '';
  if(Trim(eval('document.'+formname+'.'+textboxname+'.value'))=='') {
    localerror =  '- '+displaytext+' is Required.\n';
  } else localerror = '';
  return localerror;
}

function checkNum(formname, textboxname, displaytext) {
  var localerror = '';
  if(isNaN(eval('document.'+formname+'.'+textboxname+'.value'))) {
    localerror =  '- '+displaytext+' Should Be A Number With No Spaces.\n';
  } else localerror = '';
  return localerror;
}

function checkSpaces(formname, textboxname, displaytext) {
  var valid = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_'; // define valid characters
  var localerror = '';
  if(!isValid(Trim(eval('document.'+formname+'.'+textboxname+'.value')), valid)) {
    localerror =  '- '+displaytext+' Should Not Contain Spaces.\n';
  } else localerror = '';
  return localerror;
}

function checkSelect(formname, selectboxname, displaytext) {
  var localerror = '';
  if(eval('document.'+formname+'.'+selectboxname+'.selectedIndex')==0) {
    localerror =  '- '+displaytext+' is Required.\n';
  } else localerror = '';
  return localerror;
}

function getRadio(formname, radioname, displaytext) {
  for (var i=0; i < eval('document.'+formname+'.'+radioname+'.length'); i++) {
    if (eval('document.'+formname+'.'+radioname+'[i].checked')) {
      var rad_val = eval('document.'+formname+'.'+radioname+'[i].value');
      return rad_val;
    }
  }
}

function checkRadio(formname, radioname, displaytext) {
  var localerror = '';
  var rad_val    = '';
  for (var i=0; i < eval('document.'+formname+'.'+radioname+'.length'); i++) { //check every radio button by that name
    if (eval('document.'+formname+'.'+radioname+'[i].checked'))  { //if it is checked
      rad_val += '-';
      }	else rad_val += '';
      }
    if (rad_val=='') {
      localerror =  '- '+displaytext+' is Required.\n';
    }
  return localerror;
}

function autoComplete (field, select, property) {
/*onKeyUp="autoComplete(this,this.form.selectboxname,'value',false)" - add this to textbox where you are typing*/
  var found = false;
  for (var i = 0; i < select.options.length; i++) {
    if (select.options[i][property].toUpperCase().indexOf(field.value.toUpperCase()) == 0) {
      found=true; break;
    }
  }
  if (found) {
    select.selectedIndex = i;
  } else {
    select.selectedIndex = -1;
  }
  if (field.createTextRange) {
    if (!found) {
      field.value=field.value.substring(0,field.value.length-1);
      return;
    }
    var cursorKeys ="8;46;37;38;39;40;33;34;35;36;45;";
    if (cursorKeys.indexOf(event.keyCode+";") == -1) {
      var r1 = field.createTextRange();
      var oldValue = r1.text;
      var newValue = found ? select.options[i][property] : oldValue;
      if (newValue != field.value) {
        field.value = newValue;
        var rNew = field.createTextRange();
        rNew.moveStart('character', oldValue.length) ;
        rNew.select();
      }
    }
  }
}

function Trim(s) {
  while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r')) {
    s = s.substring(1,s.length);
  }
  while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r')) {
    s = s.substring(0,s.length-1);
  }
  return s;
}

function isValid(string,allowed) {
//  var valid = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; // define valid characters
    for (var i=0; i< string.length; i++) {
      if (allowed.indexOf(string.charAt(i)) == -1) return false;
    }
    return true;
}

Link to comment
Share on other sites

jsut reading through breifly

 

<input type="submit" value="Close" onClick="return check('updaterecord', this.name)"/>

 

this.name is the name of the submit button which in this case is undefined. I will edit post when I have read though the rest of the script.

 

--------------

 

to stop the form submitting try adding "return false;" to the function

<input type="submit" value="Close" onClick="return check('updaterecord', this.name); return false;"/>

not sure if it works no forms but its how you stop links in hrefs - worth a try though.

 

--------------

 

If all you want is a message if the feild is not filled it. Just have a check in the submit button.

 

  <input type="button" value="Close" onClick="if(document.updaterecord.close_reason == ''){ alert('Specify action done for this notification'); }else{ document.updaterecord.submit(); }"/>

 

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.