Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-04-2012, 12:05 PM   PM User | #1
prajwala
New Coder

 
Join Date: Feb 2007
Posts: 67
Thanks: 7
Thanked 0 Times in 0 Posts
prajwala is an unknown quantity at this point
Question How to use varioable in reguler expression???

I have called a function with passing 2 arguments in it.i is for id and other is for number.

I want to use that number in reguler expression.

Please find below code:

<html>
<head>
<script type="text/javascript">
function checkNumber(id,num){
var elementTxt=document.getElementById(id);
var a=parseInt(num);
var re5digit='/^\d{'+num+'}$/'; //regular expression defining a 5 digit number


if(elementTxt.value!=null || elementTxt.value!=""){
if (elementTxt.value.search(re5digit)==-1 ) //if match failed
{
alert("Please enter a valid 5 digit number inside form");
//return false;
}
}
}
</script>
</head>
<body>
<input type="text" id='txt'/>
<input type="button" value="ok" onclick="checkNumber('txt',5)"/>
</body>
</html>
prajwala is offline   Reply With Quote
Old 10-04-2012, 01:18 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
Your variable re5digit is of type "string" and not of type "RegExp".
Code:
var re5digit = new RegExp('/^\d{'+num+'}$/');
devnull69 is offline   Reply With Quote
Old 10-04-2012, 01:43 PM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Regular expressions can be handy for a lot of things, but for simpler tasks they are not the most efficient way. I would suggest determining if the field is not a number and is of a certain length:-


Code:
<html>
<head>

<script type="text/javascript">

function checkNumber(id,num) {
// num is a number already so no need for parseInt(), which in any case ought to specify the radix
var elementTxt = Number(document.getElementById(id).value);
if ((isNaN(elementTxt))  || (elementTxt.toString().length != num)) {
alert ("Please enter a valid " + num + "-digit number inside form");
document.getElementById(id).value = "";  // clear the field
return false;
}
}

</script>

</head>
<body>
<input type="text" id='txt' size = "5" maxlength = "5"/>
<input type="button" value="Check It" onclick="checkNumber('txt',5)"/>

</body>
</html>
The value of a form field can never be null. Only "" (blank).

You ought to know that when posting here you are asked to help us to help you by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.





Protestors Tried To Spoil Play But Actors Succeeded - Headline in Surrey Adveriser
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 10-04-2012 at 02:18 PM..
Philip M is offline   Reply With Quote
Old 10-04-2012, 01:58 PM   PM User | #4
prajwala
New Coder

 
Join Date: Feb 2007
Posts: 67
Thanks: 7
Thanked 0 Times in 0 Posts
prajwala is an unknown quantity at this point
Thumbs up

Hey thanks all.
It worked
prajwala is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:47 AM.


Advertisement
Log in to turn off these ads.