I'm trying to suggest username for sign up page with jquery. im just wondering how can i get the value of email address before @
sign. right now i can get complete value with
$("#email").val();
s开发者_运维知识库hould i split it by @
or some other way?
you can split a string using the split prototype function like so:
var Email = $("email").val();
var Suggestion = Email.split("@")[0]; // [0] for the first portion of the string.
@Resource : http://www.tizag.com/javascriptT/javascript-string-split.php
as this is a prototype of the string it can be used on any string like so:
var world = "Hello World".split(" ")[0];
regardless weather its set to a variable, or the value has come from a function, you would also be able to directly run after the val()
method as it returns a string.
$("#email").val().split("@")[0]
//---------------^ String is returned here.
var local = $('#email').val().split('@')[0];
//alert(local)
Splitting by "@" it's perfect, if the email address is valid (ex. it match a regexp). It's not allowed more than one time in the address, so you are on the safe side. You can find more information on this topic here
精彩评论