Javascript在验证后填充表单

Javascript在验证后填充表单,javascript,forms,Javascript,Forms,我正在为我的javascript类做一个表单,我被它的某个部分卡住了。我有一个单独的验证程序javascript文件,并调用html文件上的函数。如果未填写表单区域,则所有验证工作均有效。我想做的是,如果字段留空,它们将无法通过验证,并将在该字段中插入一个值。下面是表单字段、html页面中的javascript函数和外部验证器js文件的示例 html头中的调用函数: function formvalidation(thisform) { with (thisform) { if (textbox

我正在为我的javascript类做一个表单,我被它的某个部分卡住了。我有一个单独的验证程序javascript文件,并调用html文件上的函数。如果未填写表单区域,则所有验证工作均有效。我想做的是,如果字段留空,它们将无法通过验证,并将在该字段中插入一个值。下面是表单字段、html页面中的javascript函数和外部验证器js文件的示例

html头中的调用函数:

function formvalidation(thisform) {
with (thisform) {
if (textbox_validation(first_name,"Please enter your first name.")==false)
{first_name.blur(); return false;};
if (textbox_validation(business_name,"Please enter your business. Please enter N/A if 
you do not have one.")==false) { business_name.focus(); return false; 
business_name.value=="N/A";};
外部js验证程序:

function textbox_validation(entered, alertbox) {
with (entered) {
if (value==null || value=="") {
  alert(alertbox);
  return false;
}
else {
  return true;
   }
  }
}

所以验证器工作并关注空字段,但是对于我的一些字段,如果验证失败或者没有填充int,我希望它们自己填充一个特定的值。代码的业务名称行是在我尝试使其工作时。非常感谢您的帮助

使用DOM为字段设置占位符。像这样

 var myInput = document.getElementById('input1');
 myInput.placeholder = 'This validation has failed.';

通常,您不会使用警报,而是将错误消息放在
span
div
输入附近或
表单的顶部(或底部)。另外(如所述)它是
请尝试以下方法:

function textbox_validation(entered, errormsg) {
    var errbox = document.getElementById(entered.id + '-errors'); // just to prevent writing it twice
    // Note this requires the input to have an id, and the errer box's id to be the same with an '-errors' suffix.

    // Instead of using with, just acces properties normally
    if (!entered.value) { // The `!` "neggation" operater makes "falsy" values `true`
                      // "falsy" values include `false`, the empty string, `0`, `null`, `undefined`, `NaN` and a few others
        // Put the error message in the DOM instead of alerting it
        errbox.innerHTML = errormsg;
        return false;
    }
    else {
        // Wipe any previous error messages
        errbox.innerHTML = '';
        return true;
    }
}
同样,对于表单验证程序;我们不要将
一起使用。但是,当尝试将“N/A”分配给值时,您使用了比较运算符而不是赋值运算符,并且在返回后执行了

function formvalidation(thisform) {
    // just use the `!` "negation" operator
    if (!textbox_validation(thisform.first_name,
        "Please enter your first name."))
    {
        thisform.first_name.blur();
        return false;
    }
    if (!textbox_validation(business_name,
        "Please enter your business. Please enter N/A if you do not have one."))
    {
        thisform.business_name.focus();
        thisform.business_name.value = "N/A"; // for assignment, use `=`. `==` and `===` are used for comparison
        return false; // a return statement ends the function, make sure it's after anything you want to execute!
    }
}

通常不鼓励将
一起使用。来源:谢谢你的快速回复。我应该把代码放在HTML或外部JS文件的什么位置?input1将是我的字段id,比如first_name?将它放在
警报(alertBox)之后的外部js文件中。是的,input1是指您将为要显示的输入字段设置的id。请记住,每个输入字段必须有一个不同的id。我刚刚注意到你的帖子,我刚刚读到关于不使用with的内容。我会用这个来代替。如何为每个输入创建和显示不同的错误消息?@user2532765要创建错误消息,只需将其传递给textbox_validate(就像您已经在做的那样)。要创建错误消息框
HTML
,请尝试
注意类似的
id
s和
getElementById(entered.id+'-errors')
显然,您会将
foo
更改为已用于每个输入的
id