JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 9th, 2013, 07:03 PM
dudskie37 dudskie37 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 1 dudskie37 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 40 sec
Reputation Power: 0
Post How to make allowed values are 1 to 7, n and blank only?

I am just new with javascript and I only need to allow values when submitted. Allowed values are 1 to 7, n and blank only... How to restrict others? I just started below to allow 1 to 7 but for n and blank, I don't know yet.

javascript:
Code:
function verify(frm) {
	if (frm.sc_scores.value > 7 || frm.sc_scores.value <= 0) {
		self.alert(frm.sc_scores.value + " is an invalid value! Allowed values are 1 to 7, n and blank");
		frm.sc_scores.focus();
	}
}



HTML:
Code:
<input type="text" name="sc_scores" id="sc_scores" maxlength="1" size="2" value="<%= number %>" />

Code:
<input type="button" onClick="verify(this.form)" value="Edit" />


Please help... Thanks....

Reply With Quote
  #2  
Old February 9th, 2013, 08:07 PM
web_loone08's Avatar
web_loone08 web_loone08 is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2008
Posts: 599 web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 12 h 40 m
Reputation Power: 69
Code:
<script>

function verify(frm_field)
{

var allowed = /^[1-7n\s]{1}$/;

var field = document.getElementById(frm_field).value;

if (field.match(allowed)) {
  document.frm.onsubmit = function() {
   return true;
 }
}
else if (field.length == 0) {
  document.frm.onsubmit = function() {
   return true;
 }
}
else {
 alert(field + " is an invalid value! Allowed values are 1 to 7, n and blank");
}

}

</script>


<form name="frm" onsubmit="return false">
<input type="text" name="sc_scores" id="sc_scores" maxlength="1" size="2" value="<%= number %>"/>
<input type="submit" onClick="verify('sc_scores')" value="Edit" />
</form>

Reply With Quote
  #3  
Old February 10th, 2013, 03:15 AM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,834 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 1 h 44 m 46 sec
Reputation Power: 811
Hi,

@ web_loone08:

Do you run a "code for free" service now?

Might be a better idea to explain the approach rather than post solutions. This way the OP might actually learn something apart from copying and pasting stuff from the internet (I'm sure there are better places for that).

By the way, the "{1}" in the regex is useless. A pattern repeated once is obviously just the pattern.
Comments on this post
web_loone08 agrees: Ikr,

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > How to make allowed values are 1 to 7, n and blank only?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap