开发者

How to validate this type of javascripts

开发者 https://www.devze.com 2023-01-29 22:15 出处:网络
if input string is like 123karthik alert (\'correct format\'); else if input string is like 开发者_运维百科

if input string is like

123karthik

alert ('correct format');

else if input string is like

开发者_运维百科

karthik123

alert('Invalid format')


var regex = /[0-9]+[a-z]+/;
regex.test("karthik123") ? alert("yep") : alert("oops");


Try this... It works for me :) I don't know why people kept marking your question down. It was pretty simple to understand what you wanted.

<!DOCTYPE html>

<html>
<head>
<script language="JavaScript1.2">
function checkMe(){
var numChar=/^\d{3}\w+$/ 
if (document.myform.myinput.value.search(numChar)) 
alert("Please enter valid input inside form")
}
</script>
</head>
<body>

<form name="myform">
<input type="text" name="myinput" size=15>
<input type="button" onClick="checkMe()" value="check">

</form>


</body>
</html> 


Looks like you want to use a regular expression. That's the most general (although sometimes also the most confusing) way to validate string data.


To check the input string you'd need something like this: (because you seem new to javascript I'll only give you hints to help you learn)

var user_input = prompt("Please enter the input string"); 
if (user_input == "123karthik");
     alert('correct format');
else ...

I assume you can handle it on after the else!

NOTE: The code above is incomplete and is only for guidance.

0

精彩评论

暂无评论...
验证码 换一张
取 消