473,473 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

startswith function in javascript

1 New Member
my form has a textbox that accepts input from the user. Now i want my javascript to check the value from the textbox if it starts with any specified characters. How can i do this in javascript? please help...
May 3 '07 #1
3 14330
r035198x
13,262 MVP
my form has a textbox that accepts input from the user. Now i want my javascript to check the value from the textbox if it starts with any specified characters. How can i do this in javascript? please help...
You can use a regular expression for it.
May 3 '07 #2
acoder
16,027 Recognized Expert Moderator MVP
If you know about prototype and the ability to add methods to the String object directly, try this:
Expand|Select|Wrap|Line Numbers
  1. String.prototype.startsWith = function(str) {return (this.match(”^”+str)==str)}
Then use as a normal method of the String object.
May 3 '07 #3
mrhoo
428 Contributor
Both r0's and acoder's solutions are good. I combine them-

String.prototype.begin= function(c){
return (c)? (this.search(c)== 0): this.charAt(0);
}

You can pass nothing, and get the first character,
or a string or regular expression, and get a true or false if the string starts with the argument passed.
May 3 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

39
by: Erlend Fuglum | last post by:
Hi everyone, I'm having some trouble sorting lists. I suspect this might have something to do with locale settings and/or character encoding/unicode. Consider the following example, text...
1
by: Robin Tucker | last post by:
Hi, I would like to select records from a table where the following criteria hold: SELECT * from Mytable where field x "contains" string @X or
6
by: LongBow | last post by:
Hello all, I am having a little problems getting String's StartsWith and EndsWith methods. If I have a string defined as sNormalPt which equals "0x11D0" then use the following command ...
1
by: Dino Buljubasic | last post by:
Hi, I am using lots of string comparisons using String.StartsWith(subString) method where subString might be more than just couple of chars (e.g. "This is the string to compare ") I have...
1
by: Brian Cole | last post by:
I need to iterate through a file, checking whether each line 'startswith()' a string that belongs to a set. Normally, the most efficient way I would do this would be: strs=set() for line in...
5
by: metaperl | last post by:
I just finished answering a question in #python because someone tried to match using ... well.. match() but did not realize that match() is actually startswith() for regexps. I suggest:...
4
by: CCLeasing | last post by:
I have a text file which consists of lines of data. Each line has units of data seperated by semicolons, like so. AAAA ; some text relating to AAAA ; some more text relating to AAAA BBBB ; some...
11
by: cjt22 | last post by:
Hi startswith( prefix]) States: Return True if string starts with the prefix, otherwise return False. prefix can also be a tuple of suffixes to look for. However when I try and add a tuple...
4
by: zacks | last post by:
I need to check the current value of a string value to see if it either starts with or ends with a double quote character, as in "123" (where the quotes are actual contens of the string). I thought...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.