我正在尝试创建一个系统用户名,该用户名由姓氏,街道地址,名字中的第一个字母字符组成;这个月的数字日;和提交时间的数字秒字段。目前我有这个,它没有所有的地址代码(gname和surname)。
function validateForm()
{
var system= '';
var givenname= document.getElementById('gname').value;
var familyname= document.getElementById('surname').value;
var addy= document.getElementById('address').value;
addy = addy.replace(/[0-9]/g, "");
var givchar = givenname.substr(0, 1);
var famchar = familyname.substr(0, 1);
var addchar = addy.substr(0, 1);
system += famchar+givchar+addchar;
document.getElementById('susername').value=system;
}
如果我删除以下内容:
var addy= document.getElementById('address').value;
addy=addy.replaceAll("[0-9]","");
var addchar = addy.substr(0, 1);
然后提取正确的索引,否则此时susername文本框中不显示任何内容。
<form id="rego" action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" onSubmit="return validateForm()">
<label>Given Name:</label> <input type="text" id="gname" name="gname"><br />
<br />
<label>Middle Name: </label><input type="text" name="mname"><br />
<br />
<label>Family Name:</label> <input type="text" id="surname" name="surname"><br />
<br />
<label>Chosen Username:</label> <input type="text" name="username"><br />
<br />
<label>Address:</label> <input type="text" id="address" name="address"><br />
<br />
<label>Postcode: </label><input type="text" name="postcode"><br />
<br />
<label>State:</label> <input type="text" name="state"><br />
<br />
<label>Tel number: </label><input type="text" name="tel"><br />
<br />
<label>Password:</label> <input type="password" name="password" value="<?php
echo $passw;?>"/><span class="error">* <?php echo $passErr;?></span><br />
<br />
<label>Password confirmation:</label> <input type="password" name="passconfirm" value="<?php
echo $passw1;?>"/><span class="error">* <?php echo $pass1Err;?></span><br />
<br />
<label>System username</label> <input type="text" name="susername" id="susername" >
<br />
<input type="submit" value="submit" name="submit">
</div>
</form>
答案 0 :(得分:2)
尝试更改
addy=addy.replaceAll("[0-9]","");
带
addy = addy.replace(/[0-9]/g, "");
或者,要获得第一个字母(不是数字,符号等),请使用:
addy = addy.replace(/[^A-Za-z]/g, "");
祝你好运!