Skip to main content

Bug Tracker

Side navigation

#14713 closed bug (duplicate)

Opened January 22, 2014 03:26AM UTC

Closed January 23, 2014 04:48PM UTC

possible regex/keyup bug with Chrome?

Reported by: icampbell2@student.gsu.edu Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.10.2
Keywords: Cc:
Blocked by: Blocking:
Description

I have encountered a regex problem when using jQuery's latest minified version with Chrome version 32.

The code I have (see below) is restricting the user's input to a numbers from -99 to 99, with a keyup event-listener.

In Chrome, try starting the input with "--" or maybe "1-" and the input restriction breaks (then allowing the user to type whatever, even letters, length unrestricted). Note that no error is occurring in either Firefox 26 or IE 11.

Upon running the code in Chrome, this error is output to the console:

event.returnValue is deprecated. Please use the standard event.preventDefault() instead. jquery-latest.js:5374

Here is the code:

<!DOCTYPE html>
<html>
	<head>
		<title>Regex Testing</title>
		
		<style type="text/css">
			div {
				background-color: gray;
				text-align: center;
				margin-left: auto;
				margin-right: auto;
				width: 500px;
				height: 100px;
				display: table;
				
				p {
					background-color: white;
				}
				
				p, input {
					display: table-cell;
					vertical-align: middle;
				}
			}
		</style>
	</head>
	<body>
		<div>
			<p>Enter a 2 digit number, positive or negative:<p>
			<input type="number" id="answer" placeholder="enter a number..." />
		</div>
		
		<!-- jQuery imported here: -->
		<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
		
		<script type="text/javascript">
			// NOTE: only keyup can determine the CURRENT number of characters in an input field:
			$("#answer").keyup(function () {
				// allowing numbers and dashes only:
				var data = $(this).val();
				var regx = "^\\-?[1-9]{0,1}[0-9]{0,1}$";

				if (data == "" || data.match(regx)) {
				} else {
					$("#answer").val(data.substr(0, data.length - 1));
				}
			});
		</script>
	</body>
</html>

Thanks for an awesome product though, I love jQuery!

Attachments (0)
Change History (1)

Changed January 23, 2014 04:48PM UTC by timmywil comment:1

resolution: → duplicate
status: newclosed

Duplicate of #14282.