View Full Version : مشکل در JavaScript
behrooz4859
8 July 2005, 12:00 PM
سلام
من یه فیلد دارم میخوام فقط توش بشه عدد تایپ کرد یا بر عکس فقط توش بشه حروف تایپ کرد نشه عدد نوشت !!!
و اینکه وقتی طرف اومد تایپ کرد بهش Error بده که نمیشه حروف بنویسه فقط عدد بنویس.
من خیلی لنگ این اسکریپ هستم لطفا کمکم کنید.
هر چه سریعتر بهتر ممنونم.
behrooz4859
8 July 2005, 02:13 PM
کسی کمک نمیکنه ؟!
ninijapoli
8 July 2005, 02:28 PM
آقا یه نگاه به اینا بنداز :
http://www.irt.org/script/332.htm
payamix
8 July 2005, 02:46 PM
<SCRIPT LANGUAGE="JavaScript">
<!-- JavaScript
//---------------------------------------------------------------------------------------------------------------------
//----------------------------------------Number Formatter by ProWebMasters.net----------------------------------------
//---------------------------------------- You are free to use this in your ----------------------------------------
//---------------------------------------- scripts, but you must leave this ----------------------------------------
//---------------------------------------- Copyright notice intact. Enjoy! ----------------------------------------
//---------------------------------------------------------------------------------------------------------------------
//---------------------------------------- You can call this function with only----------------------------------------
//---------------------------------------- the 'num' object (reference your ----------------------------------------
//---------------------------------------- field you want to format). If no ----------------------------------------
//----------------------------------------formats are sent, it will use default----------------------------------------
//---------------------------------------- formats. ----------------------------------------
//---------------------------------------------------------------------------------------------------------------------
function FormatNumber(num, format, shortformat)
{
if(format==null)
{
// Choose the default format you prefer for the number.
//format = "#-(###) ###-#### "; // Telephone w/ LD Prefix and Area Code
format = "(###) ###-#### "; // Telephone w/ Area Code
//format = "###-###-####"; // Telephone w/ Area Code (dash seperated)
//format = "###-##-####"; //Social Security Number
}
//---------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------
if(shortformat==null)
{
// Choose the short format (without area code) you prefer.
//If you do not want multiple formats, leave it as "".
//var shortformat = "###-#### ";
var shortformat = "";
}
//---------------------------------------------------------------------------------------------------------------------
//----------------------------------------This code can be used to format any number. ---------------------------------
//----------------------------------------Simply change the format to a number format ---------------------------------
//---------------------------------------- you prefer. It will ignore all characters ---------------------------------
//---------------------------------------- except the #, where it will replace with ---------------------------------
//---------------------------------------- user input. ---------------------------------
//---------------------------------------------------------------------------------------------------------------------
var validchars = "0123456789";
var tempstring = "";
var returnstring = "";
var extension = "";
var tempstringpointer = 0;
var returnstringpointer = 0;
count = 0;
// Get the length so we can go through and remove all non-numeric characters
var length = num.value.length;
// We are only concerned with the format of the phone number - extensions can be left alone.
if (length > format.length)
{
length = format.length;
};
// scroll through what the user has typed
for (var x=0; x<length; x++)
{
if (validchars.indexOf(num.value.charAt(x))!=-1)
{
tempstring = tempstring + num.value.charAt(x);
};
};
// We should now have just the #'s - extract the extension if needed
if (num.value.length > format.length)
{
length = format.length;
extension = num.value.substr(format.length, (num.value.length-format.length));
};
// if we have fewer characters than our short format, we'll default to the short version.
for (x=0; x<shortformat.length;x++)
{
if (shortformat.substr(x, 1)=="#")
{
count++;
};
}
if (tempstring.length <= count)
{
format = shortformat;
};
//Loop through the format string and insert the numbers where we find a # sign
for (x=0; x<format.length;x++)
{
if (tempstringpointer <= tempstring.length)
{
if (format.substr(x, 1)=="#")
{
returnstring = returnstring + tempstring.substr(tempstringpointer, 1);
tempstringpointer++;
}else{
returnstring = returnstring + format.substr(x, 1);
}
}
}
// We have gone through the entire format, let's add the extension back on.
returnstring = returnstring + extension;
//we're done - let's return our value to the field.
num.value = returnstring;
}
// - JavaScript - -->
</SCRIPT>
behrooz4859
8 July 2005, 04:50 PM
اقای payamix من این کد رو باید قبل از کد فیلد بزارم دیگه ؟!! بعد Error رو همون موقع میده یا این که وقتی رو دکمه مثلا Next Page کلیک کنه Error میده که اینجا مشکل داره ؟
راستس ممنون از شما ninijapoli از این سایت.
ببین من این فیلدمه
<TD class=pplabel align=right height="22" width="219"><font size="2">Phone:</font></TD>
خوب حالا میخوام این توش فقط عدد بشه تایپ کرد حروف نشه.
این و کمک کن.
کمکم کنید خیلی مهمه برام
کسی این تو نمیاد ؟!!!!
payamix
8 July 2005, 05:02 PM
این یک function جاوا است ...
تنها کاری که میکنید اینه که فیلد مورد نزر رو به این اسکریپت ارجاع بدین ، مثلن :
<INPUT TYPE="text" NAME="phone" SIZE="26" ONKEYPRESS="FormatNumber(this, '(###) ###-#### ', '###-#### ');">
قسمت ### به هر سورتی که شما در نزر دارید قابل تنزیم است.
ninijapoli
11 July 2005, 12:12 PM
جناب بهروز این اسکریپت رو در صفحه قرار بدید :
<script language="JAVASCRIPT">
<!--
function check(contents) {
if (((contents / contents) != 1) && (contents != 0)) {
alert('Please enter only a number into this text box');
phone.value='';
}
}
//-->
</script>
این هم کد برای ساخت text box که میخواهید فقط عدد دریافت کند:
<table>
<tr>
<td class="pplabel" align="right" height="22" width="219">
<font size="2">Phone:</font></td>
<td class="pplabel" align="right" height="22" width="219">
<input type="text" name="phone" onkeyup="check(this.value)"> </td>
</tr>
</table>
توجه کنید که من اسم text box رو کلمه phone گذاشتم . اگر شما خواستید عوضش کنید ، لطفن در اسکریپت اولی هم این تغییر رو اعمال کنید .
امیدوارم که بدردتون بخوره. :icon_cool
behrooz4859
31 December 2005, 02:24 PM
ممنون مشکلم حل شد.
vBulletin v3.7.1, Copyright ©2000-2008, Jelsoft Enterprises Ltd.