我正在尝试向当前正常运行的表单添加其他验证要求。
现在javascript检查以确保表单在提交之前具有以下值。
必须在此字段中输入名字。
必须在此字段中输入姓氏。
必须在此输入ID号。该值必须以“V”开头,后跟“0”(零),后跟七个数字。
必须在此字段中输入用户名。
我关心的是#3。如果此字段的值不以“V”开头,后跟“0”或“9”,然后是七个数字,我想添加一个产生对话框消息的附加要求。
例如,这些值可以起作用:
V01234567
V91234567
这些不起作用:
V0wewewew
V11234567
我尝试过改变:
if(rid.charAt(0) != 'V' || rid.charAt(1) != '0'){
到这个
if(rid.charAt(0) != 'V' || rid.charAt(1) != '0' || rid.charAt(1) != '9'){
但它不起作用。
我将不胜感激。
谢谢, 麦克
<script type="text/javascript">
function checkrequired(which) {
var fn = document.getElementById("fn").value;
var ln = document.getElementById("ln").value;
if(fn == ''){
alert("Please enter your First Name");
return false;
}
if(ln == ''){
alert("Please enter your Last Name");
return false;
}
var rid = document.getElementById("rid").value;
var nb = rid.substring(2);
if(rid.charAt(0) != 'V' || rid.charAt(1) != '0'){
alert("Enter your ID correctly: V0 followed by seven numbers.");
return false;
}else if(nb.length != 7){
alert("Enter your ID correctly: V0 followed by seven numbers.");
return false;
}else if(isNaN(nb)){
alert("Enter your ID correctly: V0 followed by seven numbers.");
return false;
}
var un = document.getElementById("un").value;
if(un == "Enter Username" || un == ''){
alert("Please make sure that you entered a valid username");
return false
}
}
</script>
</head>
<body>
<div id="wrapper">
<div id="main_content">
<FORM action="page2.html" method="POST" name=myform onSubmit="return checkrequired(this)">
<p>
Enter your first name, last name, and Identification Number (ID) into the spaces below, and then click the Continue button.
</p>
<p>
1. First Name:
<input id="fn" type="text" name="requiredFirstName" value="" size="20" maxlength="20">
</p>
<p>
2. Last Name:
<input id="ln" type="text" name="requiredLastName" value="" size="20" maxlength="20">
</p>
<p>
3. Enter your Identification Number:
<input id="rid" type="text" name="requiredID" placeholder="V0xxxxxxx" size="20" maxlength="20">
</p>
<p>
4. Enter your Username:
<input id="un" type="text" name="requiredUsername" placeholder="Enter Username" size="15" maxlength="50"><strong>@mail.mydomain.com</strong>
</p>
<p>
<input type="submit" name="Submit" value="Continue">
</p>
</FORM>
</div>
</div>
&#13;
答案 0 :(得分:1)
尝试:
function isValid(rid) {
return /^V[09]\d{7}$/.test(rid) ? rid + " is valid" : rid + " is invalid";
}
var array = [];
array.push(isValid("V01234567"));
array.push(isValid("V91234567"));
array.push(isValid("V0wewewew"));
array.push(isValid("V11234567"));
alert(array.join("\n"));
这是你的测试:
Process: Xcode [1917]
Path: /Applications/Xcode.app/Contents/MacOS/Xcode
Identifier: com.apple.dt.Xcode
Version: 6.3.2 (7718)
Build Info: IDEFrameworks-7718000000000000~2
App Item ID: 497799835
App External ID: 812404257
Code Type: X86-64 (Native)
Parent Process: ??? [1]
Responsible: Xcode [1917]
User ID: 501
Date/Time: 2015-06-19 01:33:40.608 +0530
OS Version: Mac OS X 10.11 (15A178w)
Report Version: 11
Anonymous UUID: 57E10C47-E9AB-1E21-571F-0266BDEB40A0
Sleep/Wake UUID: 5BCA9BD6-143D-4AD5-99F6-AA65136E904E
Time Awake Since Boot: 5500 seconds
Time Since Wake: 1000 seconds
Crashed Thread: 12 Dispatch queue: NSOperationQueue 0x7f9c3d58d7d0 :: NSOperation 0x7f9c3ea294e0 (QOS: USER_INITIATED)
Exception Type: EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note: EXC_CORPSE_NOTIFY
0 0x0000000109ceab6a -[IDEAssertionHandler handleFailureInMethod:object:fileName:lineNumber:assertionSignature:messageFormat:arguments:] (in IDEKit)
1 0x0000000108a445df _DVTAssertionHandler (in DVTFoundation)
2 0x0000000108a448ce _DVTAssertionFailureHandler (in DVTFoundation)
3 0x00000001093b848e +[IDEIssueManager _issueProviderInfo] (in IDEFoundation)
4 0x00000001093b7b0d -[IDEIssueManager _updateIssueProviders] (in IDEFoundation)
5 0x0000000108a573de __73-[DVTObservingBlockToken observeValueForKeyPath:ofObject:change:context:]_block_invoke (in DVTFoundation)
6 0x00000001089ccd5c DVTInvokeWithStrongOwnership (in DVTFoundation)
7 0x000000010887e3e4 -[DVTObservingBlockToken observeValueForKeyPath:ofObject:change:context:] (in DVTFoundation)
8 0x00007fff94734508 NSKeyValueNotifyObserver (in Foundation)
9 0x00007fff9474107d NSKeyValueDidChange (in Foundation)
10 0x00007fff946fa63f -[NSObject(NSKeyValueObserverNotification) didChangeValueForKey:] (in Foundation)
11 0x00007fff915ba4ff doSetValuesInSourceWithKVO (in CoreFoundation)
12 0x00007fff915ba366 _CFXPreferencesReplaceValuesInNamedVolatileSource ( (in CoreFoundation)
13 0x00007fff948e0a51 -[NSUserDefaults(NSUserDefaults) setVolatileDomain:forName:] (in Foundation)
14 0x000000011ef6ea13 -[NSUserDefaults(ITunesConnectFoundationExtensions) REPLACEMENT_setVolatileDomain:forName:] (in ITunesConnectFoundation)
15 0x000000011ef6ef87 -[NSUserDefaults(ITunesConnectFoundationExtensions) synchronizeRegistrationDomains] (in ITunesConnectFoundation)
16 0x000000011ef6ed5d -[NSUserDefaults(ITunesConnectFoundationExtensions) registerPriorityDefaults:] (in ITunesConnectFoundation)
17 0x000000011ef6069e -[MZJSONServiceClient processApplicationSettings:] (in ITunesConnectFoundation)
18 0x000000011ef60d7d -[MZJSONServiceClient connectionDidFinishLoading:] (in ITunesConnectFoundation)
19 0x00007fff8a26293e __65-[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:]_block_invoke (in CFNetwork)
20 0x00007fff8a2627b2 -[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:] (in CFNetwork)
21 0x00007fff8a2626b7 -[NSURLConnectionInternal _withActiveConnectionAndDelegate:] (in CFNetwork)
22 0x00007fff8a263734 ___ZN27URLConnectionClient_Classic26_delegate_didFinishLoadingEU13block_pointerFvvE_block_invoke (in CFNetwork)
23 0x00007fff8a33d7cd ___ZN27URLConnectionClient_Classic18_withDelegateAsyncEPKcU13block_pointerFvP16_CFURLConnectionPK33CFURLConnectionClientCurrent_VMaxE_block_invoke_2 (in CFNetwork)
24 0x00007fff85c54383 _dispatch_client_callout (in libdispatch.dylib)
25 0x00007fff85c6a672 _dispatch_block_invoke (in libdispatch.dylib)
26 0x00007fff8a1be8c4 RunloopBlockContext::_invoke_block(void const*, void*) (in CFNetwork)
27 0x00007fff915cb594 CFArrayApplyFunction (in CoreFoundation)
28 0x00007fff8a1be7bd RunloopBlockContext::perform() (in CFNetwork)
29 0x00007fff8a1be598 MultiplexerSource::perform() (in CFNetwork)
30 0x00007fff8a1be3ba MultiplexerSource::_perform(void*) (in CFNetwork)
VM Region Summary:
ReadOnly portion of Libraries: Total=470.7M resident=0K(0%) swapped_out_or_unallocated=470.7M(100%)
Writable regions: Total=1.2G written=0K(0%) resident=0K(0%) swapped_out=0K(0%) unallocated=1.2G(100%)
Model: MacBookPro9,2, BootROM MBP91.00D3.B0A, 2 processors, Intel Core i5, 2.5 GHz, 4 GB, SMC 2.2f44
Graphics: Intel HD Graphics 4000, Intel HD Graphics 4000, Built-In
Memory Module: BANK 0/DIMM0, 2 GB, DDR3, 1600 MHz, 0x80AD, 0x484D54333235533643465238432D50422020
Memory Module: BANK 1/DIMM0, 2 GB, DDR3, 1600 MHz, 0x80AD, 0x484D54333235533643465238432D50422020
AirPort: spairport_wireless_card_type_airport_extreme (0x14E4, 0xF5), Broadcom BCM43xx 1.0 (7.21.48.2.10)
Bluetooth: Version 4.4.0d69 15953, 3 services, 27 devices, 1 incoming serial ports
Network Service: Wi-Fi, AirPort, en1
Serial ATA Device: WDC WD10JPVX-22JC3T0, 1 TB
Serial ATA Device: MATSHITADVD-R UJ-8A8
USB Device: USB 2.0 Bus
USB Device: Hub
USB Device: FaceTime HD Camera (Built-in)
USB Device: USB 2.0 Bus
USB Device: Hub
USB Device: Hub
USB Device: Apple Internal Keyboard / Trackpad
USB Device: IR Receiver
USB Device: BRCM20702 Hub
USB Device: Bluetooth USB Host Controller
USB Device: USB 3.0 Bus
Thunderbolt Bus: MacBook Pro, Apple Inc., 25.1
答案 1 :(得分:0)
这个怎么样?
<script type="text/javascript">
function checkrequired(which) {
var fn = document.getElementById("fn").value;
var ln = document.getElementById("ln").value;
if(fn == ''){
alert("Please enter your First Name");
return false;
}
if(ln == ''){
alert("Please enter your Last Name");
return false;
}
var rid = document.getElementById("rid").value;
var nb = rid.substring(2);
if(rid.charAt(0) != 'V' || (rid.charAt(1) != '0' && rid.charAt(1) != '9')){
alert("Enter your ID correctly: V0 followed by seven numbers.");
return false;
}else if(nb.length != 7){
alert("Enter your ID correctly: V0 followed by seven numbers.");
return false;
}else if(isNaN(nb)){
alert("Enter your ID correctly: V0 followed by seven numbers.");
return false;
}
var un = document.getElementById("un").value;
if(un == "Enter Username" || un == ''){
alert("Please make sure that you entered a valid username");
return false
}
}
</script>
</head>
<body>
<div id="wrapper">
<div id="main_content">
<FORM action="page2.html" method="POST" name=myform onSubmit="return checkrequired(this)">
<p>
Enter your first name, last name, and Identification Number (ID) into the spaces below, and then click the Continue button.
</p>
<p>
1. First Name:
<input id="fn" type="text" name="requiredFirstName" value="" size="20" maxlength="20">
</p>
<p>
2. Last Name:
<input id="ln" type="text" name="requiredLastName" value="" size="20" maxlength="20">
</p>
<p>
3. Enter your Identification Number:
<input id="rid" type="text" name="requiredID" placeholder="V0xxxxxxx" size="20" maxlength="20">
</p>
<p>
4. Enter your Username:
<input id="un" type="text" name="requiredUsername" placeholder="Enter Username" size="15" maxlength="50"><strong>@mail.mydomain.com</strong>
</p>
<p>
<input type="submit" name="Submit" value="Continue">
</p>
</FORM>
</div>
</div>
&#13;
答案 2 :(得分:0)
尝试更改
if(rid.charAt(0) != 'V' || rid.charAt(1) != '0')
到
if(rid.charAt(0) != 'V' ||( rid.charAt(1) != '0' && rid.charAt(1) != '9'))