PDA

View Full Version : how to set first character in textboxt/text area



henro tanjung
Dec 2nd, 2014, 10:21 AM
hi all,
i'm new to javascript
so my issue is to set first character in textbox/text area

thanks in advance

Philip M
Dec 2nd, 2014, 12:06 PM
What does that mean? Give us an example of what you want.

henro tanjung
Dec 3rd, 2014, 02:35 AM
thanks for your replay Philip M

example : the text area will input nomber of phone, and the first nomber is to 8 or 9

thanks again

Old Pedant
Dec 3rd, 2014, 03:25 AM
But how will JavaScript know which to pick??? 8 or 9???

Yes, JavaScript could just pick 8 or 9 at random, but surely that is not what you want.

Arbitrator
Dec 3rd, 2014, 06:36 AM
example : the text area will input nomber of phone, and the first nomber is to 8 or 9JavaScript isn't necessary:


<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>HTML Demo</title>
</head>
<body>
<form> <!-- Add an |action| attribute and other attributes as necessary (such as |method|). -->
<fieldset>
<legend>Phone Number</legend>
<select required> <!-- Add a |name| attribute. -->
<option></option>
<option>8</option>
<option>9</option>
</select>
<input type="tel" pattern="\d+" required> <!-- Add a |name| attribute. -->
</fieldset>
<button>Submit Phone Number</button>
</form>
<!-- Add a Windows Vista (IE9) compatibility script for the |pattern| and |required| attributes here. -->
</body>
</html>

Philip M
Dec 3rd, 2014, 08:40 AM
But how will JavaScript know which to pick??? 8 or 9???

Yes, JavaScript could just pick 8 or 9 at random, but surely that is not what you want.

I think he means that he wants the first character of the entered phone number to be 8 or 9.




<input type = "text" name = "phone" id = "phone" onblur = "checkPhone(this.value)">

<script type = "text/javascript">

function checkPhone(x) {
x = Number(x) || 0;
var reg = /^8|9/;
if (!reg.test(x)) {
alert ("The phone must be a number starting with 8 or 9");
document.getElementById("phone").value = "";
return false;
}
}

</script>





I don't really understand the reference to a textarea.

It's worse than the worst case scenario. - Traffic Control Worker BBC 2

Old Pedant
Dec 3rd, 2014, 11:14 PM
JavaScript isn't necessary:
How does that HTML cause the selected number (8 or 9) to appear in the text field?

And where is the name= for that <input>, by the by?

Arbitrator
Dec 4th, 2014, 01:38 AM
I think he means that he wants the first character of the entered phone number to be 8 or 9.Yes, I think so too, but I think the code I submitted is a better alternative to what the OP is trying to accomplish. Aside from simplifying the code through the elimination of JavaScript, it's more straightforward for a user because it makes it clearer—without pre-explanation or an activated error message—what information is expected of the user.

On the other hand, I suppose JavaScript will be required anyway for Windows Vista (IE9) support. Oh well.

That said, I see an error in my pattern attribute, the value of which should read \d+ (not \d) to allow more than one digit. I'll edit the original post.



x = Number(x) || 0;
var reg = /^8|9/;
if (!reg.test(x)) {
Since x will be auto-cast as a string by the test method, I'm not sure why you converted it into a number.



alert ("The phone must be a number starting with 8 or 9");
document.getElementById("phone").value = "";IMO, it's poor form to delete the user's input when it doesn't conform to a restriction; this doesn't give them an opportunity to easily fix a mistake by modifying their existing input.


How does that HTML cause the selected number (8 or 9) to appear in the text field?It doesn't. See my reply to Philip above.


And where is the name= for that <input>, by the by?It's purposely omitted, just as I also purposely omitted the action attribute of the form element and a presumably-needed length restriction for the input element's value; I don't have enough information to supply anything other than filler content, so I opted not to bother.

Philip M
Dec 4th, 2014, 11:20 AM
Since x will be auto-cast as a string by the test method, I'm not sure why you converted it into a number.


To trap a NaN entry which starts with 8 such as 888abc
But if we knew exactly what the phone number is supposed to look like, then you could use e.g.

var reg = /^8|9\d{5,5}$/; // 6 digits starting with 8 or 9 and nothing but


IMO, it's poor form to delete the user's input when it doesn't conform to a restriction; this doesn't give them an opportunity to easily fix a mistake by modifying their existing input.

That is a matter of opinion on which we may legitimately disagree. It may well depend on the circumstances.

Arbitrator
Dec 5th, 2014, 03:48 AM
To trap a NaN entry which starts with 8 such as 888abcThat's a strange way of doing things. Given that intent, you could have simply used var reg = /^8|9\d*$/ and tested x as an unmodified string.

Philip M
Dec 5th, 2014, 08:37 AM
Given that intent, you could have simply used var reg = /^8|9\d*$/ and tested x as an unmodified string.

Yes, but I don't see that it makes an iota of difference. :)

Arbitrator
Dec 7th, 2014, 03:21 AM
Yes, but I don't see that it makes an iota of difference. :)From a functional standpoint, I suppose it doesn't. :rolleyes:


;var x = -0+"8number" || 0,
reg = function () {
return RegExp("^\x38|\x39\
[\x30-\x39]*$")
;}
''==(reg)()["test"] (x)?
alert["apply"](window, ["Meow"]
): alert.bind(self, ["☺"].shift())()
;(0)

Old Pedant
Dec 7th, 2014, 04:35 AM
Hmmm...turning your signature on its head, Arbitrator?

"For every complex answer, there is a problem that is clear, simple, and ... wrong???"

Arbitrator
Dec 7th, 2014, 05:17 AM
Hmmm...turning your signature on its head, Arbitrator?

"For every complex answer, there is a problem that is clear, simple, and ... wrong???"I suppose. This problem is moot with better form design. Which means that all of the JavaScript discussion in this thread is wasted. So, I guess I shouldn't be wasting time arguing over irrelevant minutiae.

Old Pedant
Dec 7th, 2014, 05:22 AM
Hope you know I was just kidding around. Heck, I still have no idea what ["☺"].shift() does. You confused me to pieces! LOL!

And Henro hasn't returned since the 3rd post, so I'll bet we never see him again. I think he got scared. <grin/>

Old Pedant
Dec 7th, 2014, 05:29 AM
OH OH! I figured it out! Was making it way too hard. Didn't think about the [ ] around the smiley. Tch.

Arbitrator
Dec 7th, 2014, 09:29 AM
OH OH! I figured it out! Was making it way too hard. Didn't think about the [ ] around the smiley. Tch.Hmm... I think I made the script too simple. Fixed.


// options
// 8n
// 89
;var x = +-+-"89" || -false,
reg = function () {
return RegExp("^\x38|\x39\
[\x30-"+ parseInt("9 pies in the sky")+']*$')
;}
Ω = function Ω(x) {
''==(reg)(х = { 0x0: "\ude03 \x3a Smile!", 1: "\ud83d" })["test"] (x)?
alert["apply"](window, { 0:
function (){ u=["\ud83d\udc31 \x3a Meow!"]; u[""
+ eval( "'\\x7" + ( 5>>>13 &&
'H') + "'") +
'u' +
's' +
'h'//puppy!
](!!x);
return u }}[+""]()
): alert.bind(self, х[true-false] + х[true-true]).call(top, "☘");
;}(x);

Philip M
Dec 7th, 2014, 01:37 PM
"And everyone will say
As you walk your mystic way
If that young man expresses himself in ways too deep for me
Why what a very clever young man that clever young man must be".

- W.S.Gilbert

It should be clearly understood that the purpose of this forum is not to answer questions in a way which is helpful to the OPs (often complete beginners) but to demonstrate how amazingly ******* clever and knowledgable the responder is. Is there a smiley for "contemptuous"?

I agree that the OP has probably been driven away. That has happened quite a few times before. I expect that some contributors will get a warm glow from that. "For every simple question, there is an answer which is patronising, over-complicated, convoluted, pedantic and self-congratulatory that is quite useless in the context".