• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1316
  • Last Modified:

Odd javascript behavior with this.value.replace(/[^\d]*/gi,"")

Hello,

I am having some odd behavior with this function:
onkeyup='this.onchange();' onchange='this.value=this.value.replace(/[^\d]*/gi,"");'

it is part of a form that has something like this:
Time frame 1
start date
end date
billability (which is a percentage value, but i dont want the % sign, so the above code removes anything that's not a number)

I am using the above inside the percentage input field (as seen in the code)

I have a button that adds a new timeframe, and duplicates the code of the 1st timeframe but increments the id and name of the field
and using the same function for the date...
check the example code


now the problem :
in the initial input box, the code is working fine, it removes any non integer as soon as it is typed (letters or  % or anything)

but in the 2nd (and any added ones), i cant even type a number, it's automatically removed...

can someone point out to me what's causing this? thanks
// code in time frame 1
<input type="text" name="billability_1" id="billability_1"  onkeyup='this.onchange();' onchange='this.value=this.value.replace(/[^\d]*/gi,"");' >
 
// code time frame 2 after clicking 'add time frame'
<input type="text" name="billability_2" id="billability_2"  onkeyup='this.onchange();' onchange='this.value=this.value.replace(/[^\d]*/gi,"");' >

Open in new window

0
roynaufal
Asked:
roynaufal
  • 6
  • 6
  • 4
1 Solution
 
prokvkCommented:
Err ... so the second one you pasted is like generated or what ???
0
 
roynaufalAuthor Commented:
yea
u click an "add timeframe" button,

and the input elements are dynamically inserted into the form
and the resulting code is as above (it's the actual resulting code on the page)
0
 
prokvkCommented:
Could you provide some link so I can look at it ?
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
roynaufalAuthor Commented:
http://212.36.203.182/booz/index.php
login using:  roy for username and naufal for password

then go in the menu to "staffing -> staff booking by project",
click 'add timeframe'

let me know when you check it...
because link is not permanent
0
 
prokvkCommented:
When you generate that code of your input use slashes:

<input type="text" name="billability_2" id="billability_2"  onkeyup='this.onchange();' onchange='this.value=this.value.replace(/[^\d]*/gi,\"\");' >
0
 
prokvkCommented:
Notice the slashes at the end, for the replacement
0
 
roynaufalAuthor Commented:
i did that
the version online now is bit messed , i was testing

but the code used to be the exact same javascript
i will check into it again,
this is the current JS function:

var gUniqueRowID = document.getElementById("no_rows").value; //gets row count from html form, starts with 1 and increments on each click
	current_rows = (gUniqueRowID * 1) + 1; //to parse as int, then inc
 
var gRowText = "<input type=\"text\" name=\"billability_#ROWID#\" id=\"billability_#ROWID#\"   onkeyup='this.onchange();' onchange='this.value=this.value.replace(/[^\d]*/gi,\"\");' />";
 
	str = gRowText.replace(/#ROWID#/g, "" + current_rows);
 
then the rest appends to the table

Open in new window

0
 
ZvonkoSystems architectCommented:
I changed that row to this and it worked for me:
"<input type=\"text\" name=\"billability_#ROWID#\" id=\"billability_#ROWID#\" onkeyup='if(this.value.match(/\\D/))this.value=this.value.replace(/\\D/g,\"\");' onChange='this.onkeyup()' ><span id='available_billability_#ROWID#' ></span><br />"+

Open in new window

0
 
ZvonkoSystems architectCommented:
Oh! and now I see what your problem was!

This String:
onchange='this.value=this.value.replace(/[^\d]*/gi,\"\");'

Was not a regexp; it was a String!
It should be like this:
onchange='this.value=this.value.replace(/[^\\d]*/gi,\"\");'

To veryfy my explanation type into that field the letter: D
Because of the modifier "i" you can type also the lowercase: d

<|;-)


0
 
roynaufalAuthor Commented:
ah ok
i see i see
makes sense!
thanks


i'll test it out in the morning, going to sleep now!
will let you know how things go!
0
 
ZvonkoSystems architectCommented:
Come on! Give me my pointz! I will let you know ;-)
0
 
ZvonkoSystems architectCommented:
;-)
0
 
ZvonkoSystems architectCommented:
Thanks. One more note: try to change the input only when non-digit characters were found by testing with the match() for the meta char \D for non-\d  characters.
The advantage is that you do not disturb inserting of characters into middle of the value. Every assignment of field value moves the insert cursor to the end of the field value. That is seldom necessary but when needed can be very annoying. You see?
0
 
roynaufalAuthor Commented:
10x ;)
0
 
roynaufalAuthor Commented:
aha...okay
thanks
will note that
didnt think of it before :)
0
 
ZvonkoSystems architectCommented:
you are welcome.

Shalom
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 6
  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now